/****************************** (C) COPYRIGHT 2016 RSEStudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-18
* Description        : 
*****************************************************************************************/
/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Bsp.h"
#include "main.h"
#include "r_math.h"
#include "device_pins.h"
#include "PowerManager.h"

#include "MEAS.h"
#include "W25X.h"
// #include "O2Sensor.h"
#include "xgzp6828d.h"
#include "vibration.h"
// #include "AS5311.h"
/* Private define ----------------------------------------------------------------------*/
#define MEAS_FILTER_TABLE_NBR           4
#define MEAS_FILTER_LOSE                1

/* Private macro -----------------------------------------------------------------------*/

#define ADS_REG_MUX0				0x00
#define ADS_REG_VBIAS				0x01
#define ADS_REG_MUX1				0x02
#define ADS_REG_SYS0				0x03
#define ADS_REG_OFC0				0x04
#define ADS_REG_OFC1				0x05
#define ADS_REG_OFC2				0x06
#define ADS_REG_FSC0				0x07
#define ADS_REG_FSC1				0x08
#define ADS_REG_FSC2				0x09
#define ADS_REG_IDAC0				0x0A
#define ADS_REG_IDAC1				0x0B
#define ADS_REG_GPIOCFG				0x0C
#define ADS_REG_GPIODIR				0x0D
#define ADS_REG_GPIODAT				0x0E


#define MEAS_PERIOD_COLD_POLLING	4800	//1S
#define MEAS_PERIOD_POLLING			160		//100ms


#define MEAS_EVENT_POLLING			0x0002
#define MEAS_EVENT_COLD_POLLING		0x0004
/* Extern variables --------------------------------------------------------------------*/
//__align(4)uint8_t ThermcoupleConnectStatus[TEMPERATURE_MAX_CHANNEL];
uint8_t ThermcoupleConnectStatus_Last[TEMPERATURE_MAX_CHANNEL+1];
uint8_t ChannelConnectStatus_Table[TEMPERATURE_MAX_CHANNEL+1];

int32_t cold_voltage;

#define AVG_TMP_COLD_LENGTH     3
#define AVG_TMP_COLD_LOSE       1


#define AVG_TMP_LENGTH          4
#define AVG_TMP_LOSE            1

int32_t avg_buf[8][AVG_TMP_LENGTH + 10];
int16_t adc_offset_table[8];

TaskHandle_t  meas_thread = NULL;       /* 任务句柄 */      // struct rt_thread meas_thread;
StaticTask_t  meas_thread_tcb;   
StackType_t   meas_stack[1536];  /* 任务栈空间 */

uint16_t meas_debug[32];
//rt_thread_t  tid_MEAS_Polling;

/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/
//tmosTaskID tid_meas;

int32_t Thermocouple_Temperature2Voltage(int32_t Temperature);
int32_t Thermocouple_Voltage2Temperature(int32_t Voltage);

kalman_filter_t meas_kalman[TEMPERATURE_MAX_CHANNEL+1];
kalman_filter_t meas_kalman_cold;

const int32_t Thermocouple_Table[] = {
        
   -491300, -488900, -486500, -484100, -481700, -479300, -476800, -474400, -471900, -469400,	// -150~ -141
   -466900, -464400, -461800, -459300, -456700, -454200, -451600, -449000, -446300, -443700,	// -140~ -131
   -441100, -438400, -435700, -433000, -430300, -427600, -424900, -422100, -419400, -416600,	// -130~ -121
   -413800, -411000, -408200, -405400, -402500, -399700, -396800, -393900, -391100, -388200,	// -120~ -111
   -385200, -382300, -379400, -376400, -373400, -370500, -367500, -364500, -361400, -358400,	// -110~ -101   
   -355400, -352300, -349200, -346200, -343100, -340000, -336800, -333700, -330600, -327400,	// -100~ -91
   -324300, -321100, -317900, -314700, -311500, -308300, -305000, -301800, -298600, -295300,	// -90 ~ -81
   -292000, -288700, -285400, -282100, -278800, -275500, -272100, -268800, -265400, -262000,	// -80 ~ -71
   -258700, -255300, -251900, -248500, -245000, -241600, -238200, -234700, -231200, -227800,	// -70 ~ -61
   -224300, -220800, -217300, -213800, -210300, -206700, -203200, -199600, -196100, -192500,	// -60 ~ -51   
   -188900, -185400, -181800, -178200, -174500, -170900, -167300, -163700, -160000, -156400,	// -50 ~ -41
   -152700, -149000, -145300, -141700, -138000, -134300, -130500, -126800, -123100, -119400,	// -40 ~ -31
   -115600, -111900, -108100, -104300, -100600,  -96800,  -93000,  -89200,  -85400,  -81600,	// -30 ~ -21
    -77800,  -73900,  -70100,  -66300,  -62400,  -58600,  -54700,  -50800,  -47000,  -43100,	// -20 ~ -11
    -39200,  -35300,  -31400,  -27500,  -23600,  -19700,  -15700,  -11800,   -7900,   -3900,	// -10 ~ -1    
	   000,    3900,    7900,   11900,   15800,   19800,   23800,   27700,   31700,   35700,	//  0~  9
	 39700,   43700,   47700,   51700,   55700,   59700,   63700,   67700,   71800,   75800,	// 10~ 19
	 79800,   83800,   87900,   91900,   96000,  100000,  104100,  108100,  112200,  116300,	// 20~ 29
	120300,  124400,  128500,  132600,  136600,  140700,  144800,  148900,  153000,  157100,	// 30~ 39
	161200,  165300,  169400,  173500,  177600,  181700,  185800,  189900,  194100,  198200,	// 40~ 49
	202300,  206400,  210600,  214700,  218800,  223000,  227100,  231200,  235400,  239500,	// 50~ 59
	243600,  247800,  251900,  256100,  260200,  264400,  268500,  272700,  276800,  281000,	// 60~ 69
	285100,  289300,  293400,  297600,  301700,  305900,  310000,  314200,  318400,  322500,	// 70~ 79
	326700,  330800,  335000,  339100,  343300,  347400,  351600,  355700,  359900,  364000,	// 80~ 89
	368200,  372300,  376500,  380600,  384800,  388900,  393100,  397200,  401300,  405500,	// 90~ 99
	409600,  413800,  417900,  422000,  426200,  430300,  434400,  438500,  442700,  446800,	//100~109
	450900,	 455000,  459100,  463300,  467400,  471500,  475600,  479700,  483800,  487900,	//110~119
	492000,  496100,  500200,  504300,  508400,  512400,  516500,  520600,  524700,  528800,	//120~129
	532800,  536900,  541000,  545000,  549100,  553200,  557200,  561300,  565300,  569400,	//130~139
	573500,  577500,  581500,  585600,  589600,  593700,  597700,  601700,  605800,  609800,	//140~149
	613800,  617900,  621900,  625900,  629900,  633900,  638000,  642000,  646000,  650000,	//150~159
	654000,  658000,  662000,  666000,  670100,  674100,  678100,  682100,  686100,  690100,	//160~169
	694100,  698100,  702100,  706000,  710000,  714000,  718000,  722000,  726000,  730000,	//170~179
	734000,  738000,  742000,  746000,  750000,  754000,  757900,  761900,  765900,  769900,	//180~189
	773900,  777900,  781900,  785900,  789900,  793900,  797900,  801900,  805900,  809900,	//190~199
	813800,  817800,  821800,  825800,  829800,  833800,  837800,  841800,  845800,  849900,	//200~209
	853900,  857900,  861900,  865900,  869900,  873900,  877900,  881900,  886000,  890000,	//210~219
	894000,  898000,  902000,  906100,  910100,  914100,  918100,  922200,  926200,  930200,	//220~229
	934300,  938300,  942300,  946400,  950400,  954500,  958500,  962600,  966600,  970700,	//230~239
	974700,  978800,  982800,  986900,  990900,  995000,  999100, 1003100, 1007200, 1011300,	//240~249
   1015300, 1019400, 1023500, 1027600, 1031600, 1035700, 1039800, 1043900, 1048000, 1052000,	//250~259
   1056100, 1060200, 1064300, 1068400, 1072500, 1076600, 1080700, 1084800, 1088900, 1093000,	//260~269
   1097100, 1101200, 1105300, 1109400, 1113500, 1117600, 1121700, 1125900, 1130000, 1134100,	//270~279
   1138200, 1142300, 1146500, 1150600, 1154700, 1158800, 1163000, 1167100, 1171200, 1175300,	//280~289
   1179500, 1183600, 1187700, 1191900, 1196000, 1200100, 1204300, 1208400, 1212600, 1216700, 	//290~299
   1220900, 1225000, 1229100, 1233300, 1237400, 1241600, 1245700, 1249900, 1254000, 1258200,	//300~309
   1262400, 1266500, 1270700, 1274800, 1279000, 1283100, 1287300, 1291500, 1295600, 1299800,	//310~319
   1304000, 1308100, 1312300, 1316500, 1320600, 1324800, 1329000, 1333100, 1337300, 1341500,	//320~329
   1345700, 1349800, 1354000, 1358200, 1362400, 1366500, 1370700, 1374900, 1379100, 1383300,	//330~339
   1387400, 1391600, 1395800, 1400000, 1404200, 1408400, 1412600, 1416700, 1420900, 1425100,	//340~349
   1429300, 1433500, 1437700, 1441900, 1446100, 1450300, 1454500, 1458700, 1462900, 1467100,	//350~359
   1471300, 1475500, 1479700, 1483900, 1488100, 1492300, 1496500, 1500700, 1504900, 1509100,	//360~369
   1513300, 1517500, 1521700, 1525900, 1530100, 1534300, 1538500, 1542700, 1546900, 1551100,	//370~379
   1555400, 1559600, 1563800, 1568000, 1572200, 1576400, 1580600, 1584900, 1589100, 1593300,	//380~389
   1597500, 1601700, 1605900, 1610200, 1614400, 1618600, 1622800, 1627000, 1631300, 1635500,	//390~399
   1639700, 1643900, 1648200, 1652400, 1656600, 1660800, 1665100, 1669300, 1673500, 1677800,    //400~409
   1682000, 1686200, 1690400, 1694700, 1698900, 1703100, 1707400, 1711600, 1715800, 1720100,    //410~419
   1724300, 1728500, 1732800, 1737000, 1741300, 1745500, 1749700, 1754000, 1758200, 1762400,    //420~429
   1766700, 1770900, 1775200, 1779400, 1783700, 1787900, 1792100, 1796400, 1800600, 1804900,    //430~439
   1809100, 1813400, 1817600, 1821800, 1826100, 1830300, 1834600, 1838800, 1843100, 1847300,    //440~449
   1851600, 1855800, 1860100, 1864300, 1868600, 1872800, 1877100, 1881300, 1885600, 1889800,    //450~459
   1894100, 1898300, 1902600, 1906800, 1911100, 1915400, 1919600, 1923900, 1928100, 1932400,    //460~469
   1936600, 1940900, 1945100, 1949400, 1953700, 1957900, 1962200, 1966400, 1970700, 1975000,    //470~479
   1979200, 1983500, 1987700, 1992000, 1996200, 2000500, 2004800, 2009000, 2013300, 2017500,    //480~489
   2021800, 2026100, 2030300, 2034600, 2038900, 2043100, 2047400, 2051600, 2055900, 2060200,    //490~499
   2064400, 2068700, 2073000, 2077200, 2081500, 2085700, 2090000, 2094300, 2098500, 2102800,    //500~509
   2107100, 2111300, 2115600, 2119900, 2124100, 2128400, 2132600, 2136900, 2141200, 2145400,    //510~519
   2149700, 2154000, 2158200, 2162500, 2166800, 2171000, 2175300, 2179600, 2183800, 2188100,	//520~529
   2192400, 2196600, 2200900, 2205200, 2209400, 2213700, 2217900, 2222200, 2226500, 2230700, 	//530~539
   2235000, 2239300, 2243500, 2247800, 2252100, 2256300, 2260600, 2264900, 2269100, 2273400,	//540~549   
   2277600, 2281900, 2286200, 2290400, 2294700, 2299000, 2303200, 2307500, 2311700, 2316000,	//550~559
   2320300, 2324500, 2328800, 2333100, 2337300, 2341600, 2345800, 2305100, 2354400, 2358600, 	//560~569
   2362900, 2367100, 2371400, 2375700, 2379900, 2384200, 2388400, 2392700, 2397000, 2401200,	//570~579
   2405500, 2409700, 2414000, 2418200, 2422500, 2426700, 2431000, 2435300, 2439500, 2443800,	//580~589
   2448000, 2452300, 2456500, 2460800, 2465000, 2469300, 2473500, 2477800, 2482000, 2486300,	//590~599  
   2490500, 2494800, 2499000, 2503300, 2507500, 2511800, 2516000, 2520300, 2524500, 2528800,	//600~609
   2533000, 2537300, 2541500, 2545800, 2550000, 2554300, 2558500, 2562700, 2567000, 2571200, 	//610~619
   2575500, 2579700, 2584000, 2588200, 2592400, 2596700, 2600900, 2605200, 2609400, 2613600,	//620~629
   2617900, 2622100, 2626300, 2630600, 2634800, 2639000, 2643300, 2647500, 2651700, 2656000, 	//630~639
   2660200, 2664400, 2668700, 2672900, 2677100, 2681400, 2685600, 2589800, 2694000, 2698300,	//640~649   
   2702500, 2706700, 2710900, 2715200, 2719400, 2723600, 2727800, 2732000, 2736300, 2740500, 	//650~659
   2744700, 2748900, 2753100, 2757400, 2761600, 2765800, 2770000, 2774200, 2778400, 2782600, 	//660~669
   2786900, 2791100, 2795300, 2799500, 2803700, 2807900, 2812100, 2816300, 2820500, 2824700, 	//670~679
   2828900, 2833200, 2837400, 2841600, 2845800, 2850000, 2854200, 2858400, 2862600, 2866800, 	//680~689
   2871000, 2875200, 2879400, 2883500, 2887700, 2891900, 2896100, 2900300, 2904500, 2907800, 	//690~699  
   2912900, 2917100, 2921300, 2925500, 2929700, 2933800, 2938000, 2942200, 2946400, 2950600,	//700~709
   2954800, 2958900, 2963100, 2967300, 2971500, 2975700, 2979800, 2984000, 2988200, 2992400, 	//710~719
   2996500, 3000700, 3004900, 3009000, 3013200, 3017400, 3021600, 3025700, 3029900, 3034100, 	//720~729
   3038200, 3042400, 3046600, 3050700, 3054900, 3059000, 3063200, 3067400, 3071500, 3075700, 	//730~739
   3079800, 3084000, 3088100, 3092300, 3096400, 3100600, 3104700, 3108900, 3113000, 3117200, 	//740~749 
   3121300, 3125500, 3129600, 3133800, 3137900, 3142100, 3146200, 3150400, 3154500, 3158600, 	//750~759
   3162800, 3166900, 3171000, 3175200, 3179300, 3183400, 3187600, 3191700, 3195800, 3204100,	//760~769
   3204100, 3208200,
	};
/* Private functions -------------------------------------------------------------------*/
    


void MEAS_uDelay(uint16_t x)
{
	while(x--)
	{
		__asm("nop");//for(i=0;i<1;i++);
	}
}






/*****************************************************************************************
* Function Name  : Thermocouple_Temperature2Voltage
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t Thermocouple_Temperature2Voltage(int32_t Temperature)
{
    uint16_t TableIndex;
    int32_t Voltage;
    uint16_t TablePercent;
    
    if(Temperature < -15000)
        Temperature = -15000;
    
    if(Temperature > 75000)
        Temperature = 75000;
    
    Temperature = Temperature+15000;
    TableIndex = Temperature/100;
    TablePercent = Temperature%100;
	
    Voltage = (Thermocouple_Table[TableIndex+1] - Thermocouple_Table[TableIndex])
                    *TablePercent;
    Voltage = Voltage/100 + Thermocouple_Table[TableIndex];
    return Voltage;
}
//

/*****************************************************************************************
* Function Name  : Thermocouple_Voltage2Temperature
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t Thermocouple_Voltage2Temperature(int32_t Voltage)
{
    uint8_t i,j;
    uint32_t Temp;
    uint32_t Temp_1;
    int32_t Temperature;
    
    if(Voltage<=Thermocouple_Table[0])
	{
        Voltage =  Thermocouple_Table[0];
	}
	
	if(Voltage > 3158600)
	{
		Voltage = 3158600;
	}
    
    for(i=1;i<91;i++)
    {
        if(Voltage<Thermocouple_Table[i*10])
            break;
    }
    i=i-1;
    
    for(j=0;j<10;j++)
    {
        if(Voltage<Thermocouple_Table[i*10+j])
        {
            break;
        }
    }
    j=j-1;
    
    Temp = Thermocouple_Table[i*10+j+1] - Thermocouple_Table[i*10+j];
    Temp_1 = (Voltage - Thermocouple_Table[i*10+j])*10;
    Temperature = i*100+j*10+(Temp_1/Temp)%10 - 1500;
    return Temperature;
}
//

#if (DEV_TYPE == DEV_TYPE_I7)||(DEV_TYPE == DEV_TYPE_XP)

void sen_i2c_uDelay(uint16_t x)
{
	uint8_t i;
	while(x--)
	{
		for(i=0;i<20;i++);
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
	}
}
//

void SEN_IIC_Start(void)
{    
	SEN_SCL_L();
	sen_i2c_uDelay(5);
	SEN_SDA_OUT();
	SEN_SDA_H();
	sen_i2c_uDelay(5);
	SEN_SCL_H();
	sen_i2c_uDelay(5);
	SEN_SDA_L();
	sen_i2c_uDelay(5);
	SEN_SCL_L();
    sen_i2c_uDelay(5);
    SEN_SDA_H();
}	  


void SEN_IIC_Stop(void)
{
	SEN_SCL_L();
	sen_i2c_uDelay(5);
	SEN_SDA_L();
	SEN_SDA_OUT();
    //PM_SDA_L();
 	sen_i2c_uDelay(5);
	SEN_SCL_H();
	sen_i2c_uDelay(5);
	SEN_SDA_H();
	sen_i2c_uDelay(10);							   	
}

uint8_t SEN_IIC_ReadAck(void)
{
	uint8_t Ack;

	SEN_SCL_L();
	sen_i2c_uDelay(5);	

	SEN_SDA_IN();
	//PM_SDA_H();
	sen_i2c_uDelay(5);	
	
	SEN_SCL_H();
	sen_i2c_uDelay(5);	
	if(SEN_SDA_STS()==0)
	{
		Ack = 1;
	}
	else
	{
		Ack = 0;
	}
	SEN_SCL_L();
	
	SEN_SDA_H();
	SEN_SDA_OUT();
	//PM_SDA_H();
	sen_i2c_uDelay(5);	
	return Ack;
		
}

uint8_t SEN_IIC_Wait_Ack(void)
{
	uint16_t ucErrTime=0;
	
	SEN_SCL_L();
	sen_i2c_uDelay(3);
	SEN_SDA_IN();
    sen_i2c_uDelay(1);
	SEN_SCL_H();	 
	while(SEN_SDA_STS())
	{
		sen_i2c_uDelay(1);	 
		ucErrTime++;
		if(ucErrTime>20)
		{	
			SEN_IIC_Stop();
			return 1;
		}
	}
	sen_i2c_uDelay(1);
	SEN_SCL_L();
	SEN_SDA_H();
	SEN_SDA_OUT();
    //PM_SDA_H();
	return 0;  
} 
//??ACK??
void SEN_IIC_Ack(void)
{
	SEN_SCL_L();
	sen_i2c_uDelay(2);
	SEN_SDA_L();
	SEN_SDA_OUT();
    //PM_SDA_L();
	SEN_SCL_H();
	sen_i2c_uDelay(2);
	SEN_SCL_L();
}
//???ACK??		    
void SEN_IIC_NAck(void)
{
	SEN_SCL_L();
	sen_i2c_uDelay(2);
	SEN_SDA_H();
	SEN_SDA_OUT();
    //PM_SDA_H();
	SEN_SCL_H();
	sen_i2c_uDelay(2);
	SEN_SCL_L();
}					 				     

	  
void SEN_IIC_Send_Byte(uint8_t txd)
{                        
    uint8_t t;       
    SEN_SCL_L();
	SEN_SDA_OUT();

    for(t=0;t<8;t++)
    {              
		if(txd&0x80)    
            {SEN_SDA_H();}		
		else            
            {SEN_SDA_L();}
			
		txd<<=1; 	  
		sen_i2c_uDelay(2);   
		SEN_SCL_H();
		sen_i2c_uDelay(2); 
		SEN_SCL_L();
    }	 
} 	    

   
uint8_t SEN_IIC_Read_Byte(uint8_t ack)
{
	uint8_t i,receive=0;
	SEN_SCL_L(); 
	SEN_SDA_IN();
	//PM_SDA_H();
	
	sen_i2c_uDelay(5);
    for(i=0;i<8;i++ )
	{
        SEN_SCL_L(); 
        sen_i2c_uDelay(2);
		SEN_SCL_H();
		sen_i2c_uDelay(2);
        receive<<=1;
        if(SEN_SDA_STS())
			receive++;   
    }					 
    if (!ack)
        SEN_IIC_NAck();//??nACK
    else
        SEN_IIC_Ack(); //??ACK 
	
    return receive;
}
//

uint8_t SEN_TMP117_write(uint8_t addr,uint16_t Data)
{
    uint8_t result = 0;
	SEN_IIC_Start();
	SEN_IIC_Send_Byte(0x90);
    
	result = SEN_IIC_Wait_Ack();
    if(result!=0)
        {goto end;}  
	SEN_IIC_Send_Byte(addr&0x0F);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        goto end;
       
	SEN_IIC_Send_Byte(Data>>8);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        goto end;
    
	SEN_IIC_Send_Byte(Data&0xFF);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        goto end;
    
	SEN_IIC_Stop();
    
    end:
	return result;
}


uint8_t SEN_TMP117_read(uint8_t addr,uint16_t *pData)
{
    uint8_t result = 0;
    uint16_t Data;
	SEN_IIC_Start();
	SEN_IIC_Send_Byte(0x90);
    
	result = SEN_IIC_Wait_Ack();
    if(result!=0)
        {goto end;}
	SEN_IIC_Send_Byte(addr&0x0F);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        {goto end;}
    
	SEN_IIC_Start();
	SEN_IIC_Send_Byte(0x91);
    
	result = SEN_IIC_Wait_Ack();
    if(result!=0)
        {goto end;}
    
	Data = SEN_IIC_Read_Byte(1)<<8;
    
    Data |= SEN_IIC_Read_Byte(0);
	SEN_IIC_Stop();
    
    *pData = Data;
    
    end:
	return result;
}

const uint8_t tc_channel_map[TEMPERATURE_MAX_CHANNEL] = {4,5,10,11,16,17,22,23,1,2,7,8,13,14,19,20,0,3,6,9,12,15,18,21,0};  

 
 
uint8_t ADS1220_Rdata(int32_t *pData)
{
	uint8_t OutData[4];
	int32_t Temp;
	uint8_t status;
   
    ADS_SendByte(0x12);
	ADS_SendByte(0x00);
	status = ADS_SendByte(0x00);
	
    OutData[0] = ADS_SendByte(0x00);
    OutData[1] = ADS_SendByte(0x00);
    OutData[2] = ADS_SendByte(0x00);
    
	Temp = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]); 
	
	if(Temp&0x800000)
	{
		Temp ^= 0xFFFFFF;
		Temp += 1;
        *pData = Temp *-1;
	}
	else
	{
		*pData = Temp;
	}
    return status;
}

void TC_ADS_WriteReg(uint8_t Addr,uint8_t Reg)
{
    ADS_SendByte(0x40 | Addr);
	ADS_SendByte(0x00);
    ADS_SendByte(Reg);
}

void TC_ADS_ReadReg(uint8_t Addr,uint8_t *Reg)
{
    ADS_SendByte(0x20 | Addr);
	ADS_SendByte(0x00);
    *Reg = ADS_SendByte(0xFF);
}


/*****************************************************************************************
* Function Name  : select_mux_channel
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void select_mux_channel(uint8_t ch)
{
    ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
	MEAS_uDelay(5);
		
	switch(ch)
	{
		case 0:ADS_WriteReg(0x00,0x5C);break;//2-3
		case 1:ADS_WriteReg(0x00,0x4C);break;//1-3
		case 2:ADS_WriteReg(0x00,0x2C);break;//0-3
		
		default:
		{
			ADS_WriteReg(0x00,0x2C);
		}break;
	}
    
    MEAS_uDelay(5);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();   
}

void select_adc_chip(uint8_t ch)
{
    ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
    ADS_nop();
    
    switch(ch)
    {
        case 0:ADS_CS_1_L();break;
        case 1:ADS_CS_2_L();break;
        case 2:ADS_CS_3_L();break;
        case 3:ADS_CS_4_L();break;
        case 4:ADS_CS_5_L();break;
        case 5:ADS_CS_6_L();break;
        case 6:ADS_CS_7_L();break;
        case 7:ADS_CS_8_L();break;
        
        default:
        {
            ADS_CS_1_L();
        }break;
    }
    ADS_nop();
}

uint32_t test_buf[32];
void ADS_Reset(void)
{
	uint8_t i;
    uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t OutData[4];
    int32_t Temp_32;
    uint32_t offset_buf[32];
	
	ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
	MEAS_uDelay(10);
	ADS_SendByte(0x06);
	ADS_SendByte(0x00);
	MEAS_uDelay(10);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
	
	vTaskDelay(pdMS_TO_TICKS(20));
	
	ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
	MEAS_uDelay(5);
	ADS_WriteReg(0x00,0xEC); //offset
	ADS_WriteReg(0x01,0xA4);//SPS = 600
	ADS_WriteReg(0x02,0x00);
	ADS_WriteReg(0x03,0x00);
	MEAS_uDelay(5);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
	
//	MEAS_uDelay(10000);
	
//	ADS_CS_1_L();
//	MEAS_uDelay(5);
//	ADS_ReadReg(0x00,&test_buf[0]);
//	MEAS_uDelay(5);
//	ADS_CS_1_H();
	
	vTaskDelay(pdMS_TO_TICKS(20));
    
	ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
    MEAS_uDelay(5);
	ADS_SendByte(0x08); //start
    MEAS_uDelay(5);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
        
	vTaskDelay(pdMS_TO_TICKS(20));
      
    for(i=0;i<10;i++)
    {       
        for(InCh = 0;InCh<8;InCh++)
        {
            select_adc_chip(InCh);
            
            ADS_nop();		
            
            ADS_SendByte(0x10);	
            OutData[0] = ADS_SendByte(0x00);
            OutData[1] = ADS_SendByte(0x00);
            OutData[2] = ADS_SendByte(0x00);
            ADS_nop();	
            
            Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
        
            if(Temp_32&0x800000)
            {
                Temp_32 |= 0xFF000000;
            }
            avg_buf[InCh][i] = Temp_32;      
        }
        vTaskDelay(pdMS_TO_TICKS(3));
    }
       
    for(InCh=0;InCh<8;InCh++)
    {
        Temp_32 = 0;
        for(i=0;i<10;i++)
        {
            Temp_32 += avg_buf[InCh][i];;
        }
        adc_offset_table[InCh] = Temp_32/10;
        
        if((adc_offset_table[InCh] > 1000)||(adc_offset_table[InCh] < -1000))
        {
            adc_offset_table[InCh] = 0;
        }
    } 

    select_mux_channel(0);
    vTaskDelay(pdMS_TO_TICKS(20));  
   
}

uint32_t min_value;
uint32_t timeout_value;

void ADS_GetThermocouple(void)
{
	static int16_t check_connect_timer;
	uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t avg_i,avg_j;
    int32_t Temp_32;
	int64_t Temp64;
	int16_t i,j;
    
	uint8_t ch_m;
	uint32_t Timeout;
	int32_t ADCData;
    uint8_t OutData[4];
	
	int16_t MinTemp;
	int16_t MaxTemp;
	int16_t MinChannel = 0;
	int16_t MaxChannel = 0;
	
	static int16_t MinJitterTimer;
	static int16_t MinJitterChannel;
	static int16_t MaxJitterTimer;
	static int16_t MaxJitterChannel;
    
    static int16_t insert_jitter_timer;
    static int16_t insert_jitter_channel;
    
    (void)check_connect_timer;
    
	ACC_CS_H();  
    ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();    
    
    for(chip_ch=0;chip_ch<3;chip_ch++)
    {
        select_mux_channel(chip_ch);
        vTaskDelay(pdMS_TO_TICKS(4));
        
        for(average_i=0;average_i < AVG_TMP_LENGTH;average_i++)
        {
            for(InCh = 0;InCh<8;InCh++)
            {
                ADS_nop();                
                select_adc_chip(InCh);               
                ADS_nop();		
                
                ADS_SendByte(0x10);	
                OutData[0] = ADS_SendByte(0x00);
                OutData[1] = ADS_SendByte(0x00);
                OutData[2] = ADS_SendByte(0x00);
                ADS_nop();	
                
                Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
                
                ADS_nop();
                
                ADS_CS_1_H();
                ADS_CS_2_H();
                ADS_CS_3_H();
                ADS_CS_4_H();
                ADS_CS_5_H();
                ADS_CS_6_H();
                ADS_CS_7_H();
                ADS_CS_8_H();
            
                if(Temp_32 == 0xFFFFFF)
                {
                    Temp_32 = 0x7FFFFF;
                }
                
                if(Temp_32&0x800000)
                {
                    Temp_32 |= 0xFF000000;
                }
                avg_buf[InCh][average_i] = Temp_32; 	                
            }
            vTaskDelay(pdMS_TO_TICKS(1));
        }
        
        for(i=0;i<8;i++)
        {
            for(avg_i=0;avg_i<AVG_TMP_LENGTH;avg_i++)
            {
                for(avg_j=avg_i+1;avg_j<AVG_TMP_LENGTH;avg_j++)
                {
                    if(avg_buf[i][avg_i]>avg_buf[i][avg_j])
                    {
                        Temp_32 = avg_buf[i][avg_i];
                        avg_buf[i][avg_i] = avg_buf[i][avg_j];
                        avg_buf[i][avg_j] = Temp_32;
                    }
                }
            }
            Temp64 = 0;
            for(avg_i=AVG_TMP_LOSE;avg_i<(AVG_TMP_LENGTH-AVG_TMP_LOSE);avg_i++)
            {
                Temp64 += avg_buf[i][avg_i];
            }
            Temp64 = Temp64/(AVG_TMP_LENGTH - AVG_TMP_LOSE - AVG_TMP_LOSE);
     
            ch_m = tc_channel_map[chip_ch*8 + i];
            
            if(Temp64 < 8323072)
            {
                Temp64 = Temp64 - adc_offset_table[i];
                ADCData = (Temp64*3125)>>13;
//                if(ch_m == 19)
//                {
//                    ch_m++;
//                }
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 1;
                ADCData = ADCData + cold_voltage;
                //ADCData = kalmanFilter_calc(&meas_kalman[ch_m],ADCData);
                iProfile.temperature.temperature[ch_m] = dpoint_cal(&iProfile.temperature.calib[ch_m],Thermocouple_Voltage2Temperature(ADCData));
            }
            else
            {
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 0;
                iProfile.temperature.temperature[ch_m]  = -999;
                kalmanFilter_init(&meas_kalman[ch_m],0,0.15,0.4);
            }        
        }	     
    }    
	
	MaxTemp = -999;
	MinTemp = 9999;
	
	for(InCh = 0;InCh<TEMPERATURE_MAX_CHANNEL-2  ;InCh++)
	{	
		if(InCh >= iProfile.conf.Total_channel)
		{
			break;
		}
		if(iProfile.temperature.ThermcoupleConnectStatus[InCh]!=0)
		{
			if(iProfile.temperature.temperature[InCh]>MaxTemp)
			{
				MaxChannel = InCh + 1;
				MaxTemp = iProfile.temperature.temperature[InCh];
			}
			if(iProfile.temperature.temperature[InCh]<MinTemp)
			{
				MinChannel = InCh + 1;
				MinTemp = iProfile.temperature.temperature[InCh];
			}
		}		
	}
	
	InCh=0;
	j=0;
	for(i=0;i<TEMPERATURE_MAX_CHANNEL-2;i++)
	{
		if(i >= iProfile.conf.Total_channel)
		{
			iProfile.temperature.ThermcoupleConnectStatus[i] = 0;
		}
        else
        {
            if(ThermcoupleConnectStatus_Last[i] != iProfile.temperature.ThermcoupleConnectStatus[i])
            {
                ThermcoupleConnectStatus_Last[i] = iProfile.temperature.ThermcoupleConnectStatus[i];
                if(ThermcoupleConnectStatus_Last[i] != 0)
                {
                    j = i + 1;
                }
            }
            
            if(iProfile.temperature.ThermcoupleConnectStatus[i]!=0)
            {
                InCh++;
            }	
        }
	}
	iProfile.temperature.total_connect  = InCh;
    
    if(j!=0)
    {
        insert_jitter_timer = 0;
        insert_jitter_channel = j;
    }
    
    if(insert_jitter_channel != 0)
    {
        if(iProfile.temperature.ThermcoupleConnectStatus[insert_jitter_channel - 1] !=0)
        {
            if(insert_jitter_timer++>3)
            {                
                insert_jitter_timer = 0;
                iProfile.temperature.new_inster_flag = 1;
                iProfile.temperature.last_inster = insert_jitter_channel-1;
                insert_jitter_channel = 0;
            }
        }
        else
        {
            insert_jitter_channel = 0;
        }
    }
    
	
	if(MinJitterChannel!=MinChannel)
	{
		MinJitterTimer = 0;
		MinJitterChannel = MinChannel;
		iProfile.temperature.MinTempChannel = 0;
	}
	else if(MinJitterTimer++>5)	//2s
	{
		MinJitterTimer = 30;
		iProfile.temperature.MinTempChannel = MinChannel;
		iProfile.temperature.MinTemperature = MinTemp;
	}
	
	if(MaxJitterChannel!=MaxChannel)
	{
		MaxJitterTimer = 0;
		MaxJitterChannel = MaxChannel;
		iProfile.temperature.MaxTempChannel = 0;
	}
	else if(MaxJitterTimer++>5)	//2s
	{
		MaxJitterTimer = 30;
		iProfile.temperature.MaxTempChannel = MaxChannel;
		iProfile.temperature.MaxTemperature = MaxTemp;
	}
}

int test_meas_count;

static void Task_MEAS_Polling(void *parameter)
{  
	uint16_t cold_timer;
    int16_t cold_read;
    uint8_t result;
    int32_t temp_i32;
    
    uint8_t cold_read_error = 0;
    

	
	//ADS_GetChannelStatus();
	cold_timer = 0;
    
	while(1)
	{        
        if(cold_timer++>20)
        {
            cold_timer = 0;
            
            result = SEN_TMP117_read(1,(uint16_t*)&cold_read);
            if(result == 0)
            {
                if((cold_read&(1<<13))!=0)
                {
                    result = SEN_TMP117_read(0,(uint16_t*)&cold_read);
                    if(result == 0)
                    {
                        cold_read_error = 0;
                        temp_i32 = (cold_read*100)/128;
                        temp_i32 = dpoint_cal(&iProfile.temperature.cold_calib[0],temp_i32);
                        cold_voltage = Thermocouple_Temperature2Voltage(temp_i32);      
                        iProfile.temperature.ColdTemper = temp_i32/10;
                        iProfile.sys_status.BatTempetature = iProfile.temperature.ColdTemper;                               
                    }
                    else
                    {                        
                        if(cold_read_error++>5)
                        {
                            cold_read_error = 0;
                            log_write(EFLOG_LVL_ERROR,LOG_TAG_HARD,LOG_HARD_MEAS_READ_COLD_ERR,__LINE__,0,0,0);
                            result = SEN_TMP117_write(0x01,0xC2);  //Reset chip
                            vTaskDelay(pdMS_TO_TICKS(50));
                        }
                    }            
                }
                else
                {
                    cold_read_error++;
                }
            }
            else
            {
                if(cold_read_error++>5)
                {
                    cold_read_error = 0;
                    log_write(EFLOG_LVL_ERROR,LOG_TAG_HARD,LOG_HARD_MEAS_READ_COLD_ERR,__LINE__,0,0,0);
                    result = SEN_TMP117_write(0x01,0xC2);  //Reset chip
                    vTaskDelay(pdMS_TO_TICKS(50));
                }
            }                      
        }
        
		ADS_GetThermocouple();

#if (USE_V_OXY_1 != 0)
        o2_sen_polling();
#endif
        
#if (USE_V_VACUUM_1 != 0)
        pressure_sen_polling();
#endif
		//vTaskDelay(pdMS_TO_TICKS(3));
#if (USE_VIBRATE != 0)
		vibration_polling();
#endif      
      
		test_meas_count++;
	}
}
 
#elif DEV_TYPE == DEV_TYPE_MINI
const uint8_t tc_channel_map[TEMPERATURE_MAX_CHANNEL] = {3,0,1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}; 

 
 
uint8_t ADS1621_Rdata(int32_t *pData)
{
	uint8_t OutData[4];
	int32_t Temp;
	uint8_t status;
   
    ADS_SendByte(0x12);
	ADS_SendByte(0x00);
	status = ADS_SendByte(0x00);
	
    OutData[0] = ADS_SendByte(0x00);
    OutData[1] = ADS_SendByte(0x00);
    OutData[2] = ADS_SendByte(0x00);
    
	Temp = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]); 
	
	if(Temp&0x800000)
	{
		Temp ^= 0xFFFFFF;
		Temp += 1;
        *pData = Temp *-1;
	}
	else
	{
		*pData = Temp;
	}
    return status;
}


void TC_ADS_WriteReg(uint8_t Addr,uint8_t Reg)
{
    ADS_SendByte(0x40 | Addr);
    ADS_SendByte(Reg);
}

void TC_ADS_ReadReg(uint8_t Addr,uint8_t *Reg)
{
    ADS_SendByte(0x20 | Addr);
	ADS_SendByte(0x00);
    *Reg = ADS_SendByte(0x00);
}


/*****************************************************************************************
* Function Name  : select_mux_channel
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void select_mux_channel(uint8_t ch)
{
    ADS_CS_1_L();
	ADS_nop();
		
	switch(ch)
	{
		case 0:TC_ADS_WriteReg(0x11,0x10);break;//2-3
		case 1:TC_ADS_WriteReg(0x11,0x20);break;//1-3
		case 2:TC_ADS_WriteReg(0x11,0x30);break;//0-3
        case 3:TC_ADS_WriteReg(0x11,0x40);break;//2-3
		case 4:TC_ADS_WriteReg(0x11,0x50);break;//1-3
		case 5:TC_ADS_WriteReg(0x11,0x60);break;//0-3
        case 6:TC_ADS_WriteReg(0x11,0x70);break;//2-3
		case 7:TC_ADS_WriteReg(0x11,0x80);break;//1-3
        		
		default:
		{
			TC_ADS_WriteReg(0x11,0x10);
		}break;
	}
    
    //TC_ADS_WriteReg(0x10,0X86);  //PGA : 64
    //TC_ADS_WriteReg(0x11,0x20);
    
    ADS_nop();
	ADS_CS_1_H();  
}

uint8_t test_buf[32];
void ADS_Reset(void)
{
	uint8_t i;
    uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t OutData[4];
    int32_t Temp_32;
    uint32_t offset_buf[32];
	
	ADS_CS_1_L();
	ADS_nop();
	ADS_SendByte(0x06);
	ADS_SendByte(0x00);
	ADS_nop();
	ADS_CS_1_H();

	rt_thread_delay(20);
    
    ADS_CS_1_L();
	ADS_nop();
    TC_ADS_WriteReg(0x02,0X48);  //sps=400
    TC_ADS_WriteReg(0x03,0X08);  //
    TC_ADS_WriteReg(0x04,0X00);  //
    TC_ADS_WriteReg(0x05,0X00);  //
    TC_ADS_WriteReg(0x06,0X10);  //
    
    TC_ADS_WriteReg(0x07,0X00);  //
    TC_ADS_WriteReg(0x08,0X00) ; //
    TC_ADS_WriteReg(0x09,0X00);  //
    
    TC_ADS_WriteReg(0x0A,0X00);  //
    TC_ADS_WriteReg(0x0B,0X00);  //
    TC_ADS_WriteReg(0x0C,0X40);  //
    
    TC_ADS_WriteReg(0x0D,0XFF);  //
    TC_ADS_WriteReg(0x0E,0X00);  //
    
    TC_ADS_WriteReg(0x10,0X06);  //PGA : 64
    TC_ADS_WriteReg(0x11,0XBB);  //MUX
    TC_ADS_WriteReg(0x12,0X00);  //
	
    ADS_nop();
	ADS_CS_1_H();
    
    rt_thread_delay(5);
    
	ADS_CS_1_L();
	ADS_nop();
	TC_ADS_ReadReg(0x00,&test_buf[0]);
    TC_ADS_ReadReg(0x01,&test_buf[1]);
    TC_ADS_ReadReg(0x02,&test_buf[2]);
    TC_ADS_ReadReg(0x03,&test_buf[3]);
    TC_ADS_ReadReg(0x04,&test_buf[4]);
    TC_ADS_ReadReg(0x05,&test_buf[5]);
    TC_ADS_ReadReg(0x06,&test_buf[6]);
    TC_ADS_ReadReg(0x07,&test_buf[7]);
    TC_ADS_ReadReg(0x08,&test_buf[8]);
    
    TC_ADS_ReadReg(0x11,&test_buf[8]);
	ADS_nop();
	ADS_CS_1_H();
	
	rt_thread_delay(10);
    ADS_CS_1_L();
	ADS_nop();
    ADS_SendByte(0x08);
	ADS_SendByte(0x00);
    ADS_nop();
	ADS_CS_1_H();
    
    rt_thread_delay(20);
    
    ADS_CS_1_L();
	ADS_nop();
    TC_ADS_ReadReg(0x01,&test_buf[1]);
    ADS_nop();
	ADS_CS_1_H();
    
    
//	ADS_CS_1_L();
//	ADS_CS_2_L();
//	ADS_CS_3_L();
//	ADS_CS_4_L();
//    ADS_CS_5_L();
//	ADS_CS_6_L();
//    ADS_CS_7_L();
//	ADS_CS_8_L();
//    MEAS_uDelay(5);
//	ADS_SendByte(0x08); //start
//    MEAS_uDelay(5);
//	ADS_CS_1_H();
//	ADS_CS_2_H();
//	ADS_CS_3_H();
//	ADS_CS_4_H();
//    ADS_CS_5_H();
//	ADS_CS_6_H();
//    ADS_CS_7_H();
//	ADS_CS_8_H();
//    
//	rt_thread_delay(20);
//      
//    for(i=0;i<10;i++)
//    {       
//        for(InCh = 0;InCh<8;InCh++)
//        {
//            select_adc_chip(InCh);
//            
//            ADS_nop();		
//            
//            ADS_SendByte(0x10);	
//            OutData[0] = ADS_SendByte(0x00);
//            OutData[1] = ADS_SendByte(0x00);
//            OutData[2] = ADS_SendByte(0x00);
//            ADS_nop();	
//            
//            Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
//        
//            if(Temp_32&0x800000)
//            {
//                Temp_32 |= 0xFF000000;
//            }
//            avg_buf[InCh][i] = Temp_32;      
//        }
//        rt_thread_delay(3);
//    }
       
    for(InCh=0;InCh<8;InCh++)
    {
//        Temp_32 = 0;
//        for(i=0;i<10;i++)
//        {
//            Temp_32 += avg_buf[InCh][i];;
//        }
//        adc_offset_table[InCh] = Temp_32/10;
//        
//        if((adc_offset_table[InCh] > 1000)||(adc_offset_table[InCh] < -1000))
//        {
            adc_offset_table[InCh] = 0;
//        }
    } 

    select_mux_channel(0);
    rt_thread_delay(5);   
   
}

uint32_t min_value;
uint32_t timeout_value;
uint8_t test_byte;

int32_t test_temp;
int32_t test_voltage;

int32_t test_A_temp;
int32_t test_A_voltage;

void ADS_GetThermocouple(void)
{
	static int16_t check_connect_timer;
	uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t avg_i,avg_j;
    int32_t Temp_32;
	int64_t Temp64;
	int16_t i,j;
    
	uint8_t ch_m;
	uint32_t Timeout;
	int32_t ADCData;
    uint8_t OutData[4];
	
	int16_t MinTemp;
	int16_t MaxTemp;
	int16_t MinChannel;
	int16_t MaxChannel;
	
	static int16_t MinJitterTimer;
	static int16_t MinJitterChannel;
	static int16_t MaxJitterTimer;
	static int16_t MaxJitterChannel;
    
    static int16_t insert_jitter_timer;
    static int16_t insert_jitter_channel;
    
    (void)check_connect_timer;
    
	ACC_CS_H();
    
    ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
    ADS_nop();
    
    for(chip_ch=0;chip_ch<8;chip_ch++)
    {
        select_mux_channel(chip_ch);
        
        rt_thread_delay(4);
        
        for(average_i=0;average_i < AVG_TMP_LENGTH;average_i++)
        {
            ADS_CS_1_L();
            ADS_nop();            
            ADS_SendByte(0x12);
            test_byte = ADS_SendByte(0x00);
	
            OutData[0] = ADS_SendByte(0x00);
            OutData[1] = ADS_SendByte(0x00);
            OutData[2] = ADS_SendByte(0x00);
            ADS_nop();  
            ADS_CS_1_H();
     
            Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
        
            if(Temp_32 == 0xFFFFFF)
            {
                Temp_32 = 0x7FFFFF;
            }
            
            if(Temp_32&0x800000)
            {
                Temp_32 |= 0xFF000000;
            }
            avg_buf[chip_ch][average_i] = Temp_32;             
            rt_thread_delay(1);
        }
    }
        
    for(i=0;i<8;i++)
    {
        for(avg_i=0;avg_i<AVG_TMP_LENGTH;avg_i++)
        {
            for(avg_j=avg_i+1;avg_j<AVG_TMP_LENGTH;avg_j++)
            {
                if(avg_buf[i][avg_i]>avg_buf[i][avg_j])
                {
                    Temp_32 = avg_buf[i][avg_i];
                    avg_buf[i][avg_i] = avg_buf[i][avg_j];
                    avg_buf[i][avg_j] = Temp_32;
                }
            }
        }
        Temp64 = 0;
        for(avg_i=AVG_TMP_LOSE;avg_i<(AVG_TMP_LENGTH-AVG_TMP_LOSE);avg_i++)
        {
            Temp64 += avg_buf[i][avg_i];
        }
        Temp64 = Temp64/(AVG_TMP_LENGTH - AVG_TMP_LOSE - AVG_TMP_LOSE);
 
        ch_m = tc_channel_map[i];
        
        if(Temp64 > -1300000)
        {
            Temp64 = Temp64 - adc_offset_table[i];
            ADCData = (Temp64*25000)/53687;
            iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 1;
            ADCData = ADCData + cold_voltage;
            //ADCData = kalmanFilter_calc(&meas_kalman[ch_m],ADCData);
            iProfile.temperature.temperature[ch_m] = dpoint_cal(&iProfile.temperature.calib[ch_m],Thermocouple_Voltage2Temperature(ADCData));
        }
        else
        {
            iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 0;
            iProfile.temperature.temperature[ch_m]  = -999;
            kalmanFilter_init(&meas_kalman[ch_m],0,0.15,0.4);
        }        
    }	       
	
	MaxTemp = -999;
	MinTemp = 9999;
	
	for(InCh = 0;InCh<TEMPERATURE_MAX_CHANNEL-2  ;InCh++)
	{	
		if(InCh >= iProfile.conf.Total_channel)
		{
			break;
		}
		if(iProfile.temperature.ThermcoupleConnectStatus[InCh]!=0)
		{
			if(iProfile.temperature.temperature[InCh]>MaxTemp)
			{
				MaxChannel = InCh + 1;
				MaxTemp = iProfile.temperature.temperature[InCh];
			}
			if(iProfile.temperature.temperature[InCh]<MinTemp)
			{
				MinChannel = InCh + 1;
				MinTemp = iProfile.temperature.temperature[InCh];
			}
		}		
	}
	
	InCh=0;
	j=0;
	for(i=0;i<TEMPERATURE_MAX_CHANNEL-2;i++)
	{
		if(i >= iProfile.conf.Total_channel)
		{
			iProfile.temperature.ThermcoupleConnectStatus[i] = 0;
		}
        else
        {
            if(ThermcoupleConnectStatus_Last[i] != iProfile.temperature.ThermcoupleConnectStatus[i])
            {
                ThermcoupleConnectStatus_Last[i] = iProfile.temperature.ThermcoupleConnectStatus[i];
                if(ThermcoupleConnectStatus_Last[i] != 0)
                {
                    j = i + 1;
                }
            }
            
            if(iProfile.temperature.ThermcoupleConnectStatus[i]!=0)
            {
                InCh++;
            }	
        }
	}
	iProfile.temperature.total_connect  = InCh;
    
    if(j!=0)
    {
        insert_jitter_timer = 0;
        insert_jitter_channel = j;
    }
    
    if(insert_jitter_channel != 0)
    {
        if(iProfile.temperature.ThermcoupleConnectStatus[insert_jitter_channel - 1] !=0)
        {
            if(insert_jitter_timer++>3)
            {                
                insert_jitter_timer = 0;
                iProfile.temperature.new_inster_flag = 1;
                iProfile.temperature.last_inster = insert_jitter_channel-1;
                insert_jitter_channel = 0;
            }
        }
        else
        {
            insert_jitter_channel = 0;
        }
    }
    
	
	if(MinJitterChannel!=MinChannel)
	{
		MinJitterTimer = 0;
		MinJitterChannel = MinChannel;
		iProfile.temperature.MinTempChannel = 0;
	}
	else if(MinJitterTimer++>5)	//2s
	{
		MinJitterTimer = 30;
		iProfile.temperature.MinTempChannel = MinChannel;
		iProfile.temperature.MinTemperature = MinTemp;
	}
	
	if(MaxJitterChannel!=MaxChannel)
	{
		MaxJitterTimer = 0;
		MaxJitterChannel = MaxChannel;
		iProfile.temperature.MaxTempChannel = 0;
	}
	else if(MaxJitterTimer++>5)	//2s
	{
		MaxJitterTimer = 30;
		iProfile.temperature.MaxTempChannel = MaxChannel;
		iProfile.temperature.MaxTemperature = MaxTemp;
	}
}

int test_meas_count;

static void Task_MEAS_Polling(void *parameter)
{  
	uint16_t cold_timer;
    int16_t cold_read;
    uint8_t result;
    int32_t temp_i32;
    uint8_t i;
    uint8_t cold_read_error;
    uint8_t OutData[4];
    int64_t temp_i64;
	
	//ADS_GetChannelStatus();
	cold_timer = 0;
    
	while(1)
	{        
        if(cold_timer++>20)
        {
            cold_timer = 0;
            
            ADS_CS_1_L();
            ADS_nop();           
            TC_ADS_WriteReg(0x10,0X00);  //PGA : 1
            TC_ADS_WriteReg(0x11,0xBB);
            ADS_nop();
            ADS_CS_1_L();
            
            MEAS_uDelay(5);
            
            ADS_CS_1_L();
            ADS_nop();
            ADS_SendByte(0x08);
            ADS_SendByte(0x00);
            ADS_nop();
            ADS_CS_1_H();
            
            rt_thread_delay(3);
        
            temp_i64 = 0;
            for(i=0;i < AVG_TMP_LENGTH;i++)
            {
                ADS_CS_1_L();
                ADS_nop();            
                ADS_SendByte(0x12);
                test_byte = ADS_SendByte(0x00);
        
                OutData[0] = ADS_SendByte(0x00);
                OutData[1] = ADS_SendByte(0x00);
                OutData[2] = ADS_SendByte(0x00);
                ADS_nop();  
                ADS_CS_1_H();
         
                temp_i32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
            
                if(temp_i32 == 0xFFFFFF)
                {
                    temp_i32 = 0x7FFFFF;
                }
                
                if(temp_i32&0x800000)
                {
                    temp_i32 |= 0xFF000000;
                }
                
                temp_i64 += temp_i32;      
                rt_thread_delay(1);
            }
            
            ADS_CS_1_L();
            ADS_nop();           
            TC_ADS_WriteReg(0x10,0x06);  //PGA : 64
            TC_ADS_WriteReg(0x11,0x10);
            ADS_nop();
            ADS_CS_1_L();
            
            MEAS_uDelay(5);
            
            ADS_CS_1_L();
            ADS_nop();
            ADS_SendByte(0x08);
            ADS_SendByte(0x00);
            ADS_nop();
            ADS_CS_1_H();
            
            temp_i64 = temp_i64/AVG_TMP_LENGTH;
            
            temp_i32 = temp_i64*78125/262144;
            
            temp_i32 = (temp_i32-122400)*10/42 + 2500;
            
            temp_i32 = dpoint_cal(&iProfile.temperature.cold_calib[0],temp_i32);
            cold_voltage = Thermocouple_Temperature2Voltage(temp_i32);      
            iProfile.temperature.ColdTemper = temp_i32/10;
            iProfile.sys_status.BatTempetature = iProfile.temperature.ColdTemper;         
        }                                    
        
		ADS_GetThermocouple();

#if (USE_V_OXY_1 != 0)
        o2_sen_polling();
#endif
        
#if (USE_V_VACUUM_1 != 0)
        pressure_sen_polling();
#endif
		//rt_thread_delay(3);
#if (USE_VIBRATE != 0)
		vibration_polling();
#endif      
      
		test_meas_count++;
	}
}

       
    
#elif DEV_TYPE == DEV_TYPE_VACUUM

const uint8_t tc_channel_map[TEMPERATURE_MAX_CHANNEL] = {2,5,8,9,3,6,0,10,7,1,4,11,   0,0,0,0,16,17,18,19,20,21,22,23,24}; 

void sen_i2c_uDelay(uint16_t x)
{
	uint8_t i;
	while(x--)
	{
		for(i=0;i<20;i++);
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
//		__asm("nop");
	}
}
//

void SEN_IIC_Start(void)
{    
	SEN_SCL_L();
	sen_i2c_uDelay(5);
	SEN_SDA_OUT();
	SEN_SDA_H();
	sen_i2c_uDelay(5);
	SEN_SCL_H();
	sen_i2c_uDelay(5);
	SEN_SDA_L();
	sen_i2c_uDelay(5);
	SEN_SCL_L();
    sen_i2c_uDelay(5);
    SEN_SDA_H();
}	  


void SEN_IIC_Stop(void)
{
	SEN_SCL_L();
	sen_i2c_uDelay(5);
	SEN_SDA_L();
	SEN_SDA_OUT();
    //PM_SDA_L();
 	sen_i2c_uDelay(5);
	SEN_SCL_H();
	sen_i2c_uDelay(5);
	SEN_SDA_H();
	sen_i2c_uDelay(10);							   	
}

uint8_t SEN_IIC_ReadAck(void)
{
	uint8_t Ack;

	SEN_SCL_L();
	sen_i2c_uDelay(5);	

	SEN_SDA_IN();
	//PM_SDA_H();
	sen_i2c_uDelay(5);	
	
	SEN_SCL_H();
	sen_i2c_uDelay(5);	
	if(SEN_SDA_STS()==0)
	{
		Ack = 1;
	}
	else
	{
		Ack = 0;
	}
	SEN_SCL_L();
	
	SEN_SDA_H();
	SEN_SDA_OUT();
	//PM_SDA_H();
	sen_i2c_uDelay(5);	
	return Ack;
		
}

uint8_t SEN_IIC_Wait_Ack(void)
{
	uint16_t ucErrTime=0;
	
	SEN_SCL_L();
	sen_i2c_uDelay(3);
	SEN_SDA_IN();
    sen_i2c_uDelay(1);
	SEN_SCL_H();	 
	while(SEN_SDA_STS())
	{
		sen_i2c_uDelay(1);	 
		ucErrTime++;
		if(ucErrTime>20)
		{	
			SEN_IIC_Stop();
			return 1;
		}
	}
	sen_i2c_uDelay(1);
	SEN_SCL_L();
	SEN_SDA_H();
	SEN_SDA_OUT();
    //PM_SDA_H();
	return 0;  
} 
//??ACK??
void SEN_IIC_Ack(void)
{
	SEN_SCL_L();
	sen_i2c_uDelay(2);
	SEN_SDA_L();
	SEN_SDA_OUT();
    //PM_SDA_L();
	SEN_SCL_H();
	sen_i2c_uDelay(2);
	SEN_SCL_L();
}
//???ACK??		    
void SEN_IIC_NAck(void)
{
	SEN_SCL_L();
	sen_i2c_uDelay(2);
	SEN_SDA_H();
	SEN_SDA_OUT();
    //PM_SDA_H();
	SEN_SCL_H();
	sen_i2c_uDelay(2);
	SEN_SCL_L();
}					 				     

	  
void SEN_IIC_Send_Byte(uint8_t txd)
{                        
    uint8_t t;       
    SEN_SCL_L();
	SEN_SDA_OUT();

    for(t=0;t<8;t++)
    {              
		if(txd&0x80)
			SEN_SDA_H();
		else
			SEN_SDA_L();
		txd<<=1; 	  
		sen_i2c_uDelay(2);   
		SEN_SCL_H();
		sen_i2c_uDelay(2); 
		SEN_SCL_L();
    }	 
} 	    

   
uint8_t SEN_IIC_Read_Byte(uint8_t ack)
{
	uint8_t i,receive=0;
	SEN_SCL_L(); 
	SEN_SDA_IN();
	//PM_SDA_H();
	
	sen_i2c_uDelay(5);
    for(i=0;i<8;i++ )
	{
        SEN_SCL_L(); 
        sen_i2c_uDelay(2);
		SEN_SCL_H();
		sen_i2c_uDelay(2);
        receive<<=1;
        if(SEN_SDA_STS())
			receive++;   
    }					 
    if (!ack)
        SEN_IIC_NAck();//??nACK
    else
        SEN_IIC_Ack(); //??ACK 
	
    return receive;
}
//

uint8_t SEN_TMP117_write(uint8_t chip,uint8_t addr,uint16_t Data)
{
    uint8_t result = 0;
    
	SEN_IIC_Start();
	SEN_IIC_Send_Byte(chip);
    
	result = SEN_IIC_Wait_Ack();
    if(result!=0)
        goto end;
	SEN_IIC_Send_Byte(addr&0x0F);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        goto end;
       
	SEN_IIC_Send_Byte(Data>>8);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        goto end;
    
	SEN_IIC_Send_Byte(Data&0xFF);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        goto end;
    
	SEN_IIC_Stop();
    
    end:
	return result;
}


uint8_t SEN_TMP117_read(uint8_t chip,uint8_t addr,uint16_t *pData)
{
    uint8_t result = 0;
    uint16_t Data;
	SEN_IIC_Start();
	SEN_IIC_Send_Byte(chip);
    
	result = SEN_IIC_Wait_Ack();
    if(result!=0)
        goto end;
	SEN_IIC_Send_Byte(addr&0x0F);
    
	result = SEN_IIC_Wait_Ack();
	if(result!=0)
        goto end;
    
	SEN_IIC_Start();
	SEN_IIC_Send_Byte(chip|0x01);
    
	result = SEN_IIC_Wait_Ack();
    if(result!=0)
        goto end;
    
	Data = SEN_IIC_Read_Byte(1)<<8;
    
    Data |= SEN_IIC_Read_Byte(0);
	SEN_IIC_Stop();
    
    *pData = Data;
    
    end:
	return result;
}
 
uint8_t ADS1220_Rdata(int32_t *pData)
{
	uint8_t OutData[4];
	int32_t Temp;
	uint8_t status;
   
    ADS_SendByte(0x12);
	ADS_SendByte(0x00);
	status = ADS_SendByte(0x00);
	
    OutData[0] = ADS_SendByte(0x00);
    OutData[1] = ADS_SendByte(0x00);
    OutData[2] = ADS_SendByte(0x00);
    
	Temp = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]); 
	
	if(Temp&0x800000)
	{
		Temp ^= 0xFFFFFF;
		Temp += 1;
        *pData = Temp *-1;
	}
	else
	{
		*pData = Temp;
	}
    return status;
}

void TC_ADS_WriteReg(uint8_t Addr,uint8_t Reg)
{
    ADS_SendByte(0x40 | Addr);
	ADS_SendByte(0x00);
    ADS_SendByte(Reg);
}

void TC_ADS_ReadReg(uint8_t Addr,uint8_t *Reg)
{
    ADS_SendByte(0x20 | Addr);
	ADS_SendByte(0x00);
    *Reg = ADS_SendByte(0xFF);
}


/*****************************************************************************************
* Function Name  : select_mux_channel
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void select_mux_channel(uint8_t ch)
{
    ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
	MEAS_uDelay(5);
		
	switch(ch)
	{
		case 0:ADS_WriteReg(0x00,0x5C);break;//2-3
		case 1:ADS_WriteReg(0x00,0x4C);break;//1-3
		case 2:ADS_WriteReg(0x00,0x2C);break;//0-3
		
		default:
		{
			ADS_WriteReg(0x00,0x2C);
		}break;
	}
    
    MEAS_uDelay(5);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();   
}

void select_adc_chip(uint8_t ch)
{
     ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
    ADS_nop();
    
    switch(ch)
    {
        case 0:ADS_CS_1_L();break;
        case 1:ADS_CS_2_L();break;
        case 2:ADS_CS_3_L();break;
        case 3:ADS_CS_4_L();break;
        case 4:ADS_CS_5_L();break;
        case 5:ADS_CS_6_L();break;
        case 6:ADS_CS_7_L();break;
        case 7:ADS_CS_8_L();break;
        
        default:
        {
            ADS_CS_1_L();
        }break;
    }
    ADS_nop();
}

uint32_t test_buf[32];
void ADS_Reset(void)
{
	uint8_t i;
    uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t OutData[4];
    int32_t Temp_32;
    uint32_t offset_buf[32];
	
	ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
	MEAS_uDelay(10);
	ADS_SendByte(0x06);
	ADS_SendByte(0x00);
	MEAS_uDelay(10);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
	
	rt_thread_delay(20);
	
	ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
	MEAS_uDelay(5);
	ADS_WriteReg(0x00,0xEC); //offset
	ADS_WriteReg(0x01,0xA4);//SPS = 600
	ADS_WriteReg(0x02,0x00);
	ADS_WriteReg(0x03,0x00);
	MEAS_uDelay(5);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
	
//	MEAS_uDelay(10000);
	
//	ADS_CS_1_L();
//	MEAS_uDelay(5);
//	ADS_ReadReg(0x00,&test_buf[0]);
//	MEAS_uDelay(5);
//	ADS_CS_1_H();
	
	rt_thread_delay(20);
    
	ADS_CS_1_L();
	ADS_CS_2_L();
	ADS_CS_3_L();
	ADS_CS_4_L();
    ADS_CS_5_L();
	ADS_CS_6_L();
    ADS_CS_7_L();
	ADS_CS_8_L();
    MEAS_uDelay(5);
	ADS_SendByte(0x08); //start
    MEAS_uDelay(5);
	ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
    
	rt_thread_delay(20);
      
    for(i=0;i<10;i++)
    {       
        for(InCh = 0;InCh<4;InCh++)
        {
            select_adc_chip(InCh);
            
            ADS_nop();		
            
            ADS_SendByte(0x10);	
            OutData[0] = ADS_SendByte(0x00);
            OutData[1] = ADS_SendByte(0x00);
            OutData[2] = ADS_SendByte(0x00);
            ADS_nop();	
            
            Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
        
            if(Temp_32&0x800000)
            {
                Temp_32 |= 0xFF000000;
            }
            avg_buf[InCh][i] = Temp_32;      
        }
        rt_thread_delay(3);
    }
       
    for(InCh=0;InCh<4;InCh++)
    {
        Temp_32 = 0;
        for(i=0;i<10;i++)
        {
            Temp_32 += avg_buf[InCh][i];;
        }
        adc_offset_table[InCh] = Temp_32/10;
        
        if((adc_offset_table[InCh] > 1000)||(adc_offset_table[InCh] < -1000))
        {
            adc_offset_table[InCh] = 0;
        }
    } 

    select_mux_channel(0);
    rt_thread_delay(20);   
   
}

uint32_t min_value;
uint32_t timeout_value;



void ADS_GetThermocouple(void)
{
	static int16_t check_connect_timer;
	uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t avg_i,avg_j;
    int32_t Temp_32;
	int64_t Temp64;
	int16_t i,j;
    
	uint8_t ch_m;
	uint32_t Timeout;
	int32_t ADCData;
    uint8_t OutData[4];
	
	int16_t MinTemp;
	int16_t MaxTemp;
	int16_t MinChannel;
	int16_t MaxChannel;
	
	static int16_t MinJitterTimer;
	static int16_t MinJitterChannel;
	static int16_t MaxJitterTimer;
	static int16_t MaxJitterChannel;
    
    static int16_t insert_jitter_timer;
    static int16_t insert_jitter_channel;
    
    (void)check_connect_timer;
    
	ACC_CS_H();
    
    ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
    ADS_nop();
    
    
    for(chip_ch=0;chip_ch<3;chip_ch++)
    {
        select_mux_channel(chip_ch);
        rt_thread_delay(4);
        
        for(average_i=0;average_i < AVG_TMP_LENGTH;average_i++)
        {
            for(InCh = 0;InCh<4;InCh++)
            {
                select_adc_chip(InCh);
                
                ADS_nop();		
                
                ADS_SendByte(0x10);	
                OutData[0] = ADS_SendByte(0x00);
                OutData[1] = ADS_SendByte(0x00);
                OutData[2] = ADS_SendByte(0x00);
                ADS_nop();

                ADS_CS_1_H();
                ADS_CS_2_H();
                ADS_CS_3_H();
                ADS_CS_4_H();
                ADS_CS_5_H();
                ADS_CS_6_H();
                ADS_CS_7_H();
                ADS_CS_8_H();           
                
                Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
            
                if(Temp_32 == 0xFFFFFF)
                {
                    Temp_32 = 0x7FFFFF;
                }
                
                if(Temp_32&0x800000)
                {
                    Temp_32 |= 0xFF000000;
                }
                avg_buf[InCh][average_i] = Temp_32;             
            }
            rt_thread_delay(1);
        }
        
        for(i=0;i<4;i++)
        {
            for(avg_i=0;avg_i<AVG_TMP_LENGTH;avg_i++)
            {
                for(avg_j=avg_i+1;avg_j<AVG_TMP_LENGTH;avg_j++)
                {
                    if(avg_buf[i][avg_i]>avg_buf[i][avg_j])
                    {
                        Temp_32 = avg_buf[i][avg_i];
                        avg_buf[i][avg_i] = avg_buf[i][avg_j];
                        avg_buf[i][avg_j] = Temp_32;
                    }
                }
            }
            Temp64 = 0;
            for(avg_i=AVG_TMP_LOSE;avg_i<(AVG_TMP_LENGTH-AVG_TMP_LOSE);avg_i++)
            {
                Temp64 += avg_buf[i][avg_i];
            }
            Temp64 = Temp64/(AVG_TMP_LENGTH - AVG_TMP_LOSE - AVG_TMP_LOSE);
     
            ch_m = tc_channel_map[chip_ch*4 + i];
            
            if(Temp64 < 8323072)
            {
                Temp64 = Temp64 - adc_offset_table[i];
                ADCData = (Temp64*3125)>>13;
//                if(ch_m == 19)
//                {
//                    ch_m++;
//                }
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 1;
                ADCData = ADCData + cold_voltage;
                //ADCData = kalmanFilter_calc(&meas_kalman[ch_m],ADCData);
                iProfile.temperature.temperature[ch_m] = dpoint_cal(&iProfile.temperature.calib[ch_m],Thermocouple_Voltage2Temperature(ADCData));
            }
            else
            {
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 0;
                iProfile.temperature.temperature[ch_m]  = -999;
                kalmanFilter_init(&meas_kalman[ch_m],0,0.15,0.4);
            }        
        }	     
    }    
	
	MaxTemp = -999;
	MinTemp = 9999;
	
	for(InCh = 0;InCh<TEMPERATURE_MAX_CHANNEL-2  ;InCh++)
	{	
		if(InCh >= iProfile.conf.Total_channel)
		{
			break;
		}
		if(iProfile.temperature.ThermcoupleConnectStatus[InCh]!=0)
		{
			if(iProfile.temperature.temperature[InCh]>MaxTemp)
			{
				MaxChannel = InCh + 1;
				MaxTemp = iProfile.temperature.temperature[InCh];
			}
			if(iProfile.temperature.temperature[InCh]<MinTemp)
			{
				MinChannel = InCh + 1;
				MinTemp = iProfile.temperature.temperature[InCh];
			}
		}		
	}
	
	InCh=0;
	j=0;
	for(i=0;i<TEMPERATURE_MAX_CHANNEL-2;i++)
	{
		if(i >= iProfile.conf.Total_channel)
		{
			iProfile.temperature.ThermcoupleConnectStatus[i] = 0;
		}
        else
        {
            if(ThermcoupleConnectStatus_Last[i] != iProfile.temperature.ThermcoupleConnectStatus[i])
            {
                ThermcoupleConnectStatus_Last[i] = iProfile.temperature.ThermcoupleConnectStatus[i];
                if(ThermcoupleConnectStatus_Last[i] != 0)
                {
                    j = i + 1;
                }
            }
            
            if(iProfile.temperature.ThermcoupleConnectStatus[i]!=0)
            {
                InCh++;
            }	
        }
	}
	iProfile.temperature.total_connect  = InCh;
    
    if(j!=0)
    {
        insert_jitter_timer = 0;
        insert_jitter_channel = j;
    }
    
    if(insert_jitter_channel != 0)
    {
        if(iProfile.temperature.ThermcoupleConnectStatus[insert_jitter_channel - 1] !=0)
        {
            if(insert_jitter_timer++>3)
            {                
                insert_jitter_timer = 0;
                iProfile.temperature.new_inster_flag = 1;
                iProfile.temperature.last_inster = insert_jitter_channel-1;
                insert_jitter_channel = 0;
            }
        }
        else
        {
            insert_jitter_channel = 0;
        }
    }
    
	
	if(MinJitterChannel!=MinChannel)
	{
		MinJitterTimer = 0;
		MinJitterChannel = MinChannel;
		iProfile.temperature.MinTempChannel = 0;
	}
	else if(MinJitterTimer++>5)	//2s
	{
		MinJitterTimer = 30;
		iProfile.temperature.MinTempChannel = MinChannel;
		iProfile.temperature.MinTemperature = MinTemp;
	}
	
	if(MaxJitterChannel!=MaxChannel)
	{
		MaxJitterTimer = 0;
		MaxJitterChannel = MaxChannel;
		iProfile.temperature.MaxTempChannel = 0;
	}
	else if(MaxJitterTimer++>5)	//2s
	{
		MaxJitterTimer = 30;
		iProfile.temperature.MaxTempChannel = MaxChannel;
		iProfile.temperature.MaxTemperature = MaxTemp;
	}
}

int test_meas_count;

static void Task_MEAS_Polling(void *parameter)
{  
	uint16_t cold_timer;
    int16_t cold_read;
    uint8_t result;
    int32_t temp_i32;
    
    uint8_t cold_read_error_outer;
    uint8_t cold_read_error_inner;
    
    uint8_t tmp117_err_flag_outer;
    uint8_t tmp117_err_flag_inner;

	
	//ADS_GetChannelStatus();
	cold_timer = 0;
    
	while(1)
	{        
        if(cold_timer++>20)
        {
            cold_timer = 0;
            
            result = SEN_TMP117_read(0x92,1,(uint16_t*)&cold_read);
            if(result == 0)
            {
                if((cold_read&(1<<13))!=0)
                {
                    result = SEN_TMP117_read(0x92,0,(uint16_t*)&cold_read);
                    if(result == 0)
                    {                       
                        cold_read_error_outer = 0;
                        temp_i32 = (cold_read*100)/128;
                        temp_i32 = dpoint_cal(&iProfile.temperature.cold_calib[0],temp_i32);
                        cold_voltage = Thermocouple_Temperature2Voltage(temp_i32);      
                        iProfile.temperature.ColdTemper = temp_i32/10;
                        //iProfile.sys_status.BatTempetature = iProfile.temperature.ColdTemper;      
                        tmp117_err_flag_outer = 0;                        
                    }
                    else
                    {                        
                        if(cold_read_error_outer++>5)
                        {                          
                            cold_read_error_outer = 0;                            
                            result = SEN_TMP117_write(0x92,0x01,0xC2);  //Reset chip
                            rt_thread_delay(50);
                            if(tmp117_err_flag_outer == 0)
                            {
                                log_write(EFLOG_LVL_ERROR,LOG_TAG_HARD,LOG_HARD_OUTER_TEMP_FAULT,__LINE__,0,0,0);
                            }
                            tmp117_err_flag_outer = 1;
                        }
                    }            
                }
                else
                {
                    cold_read_error_outer++;
                }
            }
            else
            {
                if(cold_read_error_outer++>5)
                {
                    cold_read_error_outer = 0;
                    result = SEN_TMP117_write(0x92,0x01,0xC2);  //Reset chip
                    rt_thread_delay(50);
                    if(tmp117_err_flag_outer == 0)
                    {
                        log_write(EFLOG_LVL_ERROR,LOG_TAG_HARD,LOG_HARD_OUTER_TEMP_FAULT,__LINE__,0,0,0);
                    }
                    tmp117_err_flag_outer = 1;
                }
            }

            result = SEN_TMP117_read(0x90,1,(uint16_t*)&cold_read);
            if(result == 0)
            {
                if((cold_read&(1<<13))!=0)
                {
                    result = SEN_TMP117_read(0x90,0,(uint16_t*)&cold_read);
                    if(result == 0)
                    {
                        cold_read_error_inner = 0;
                        temp_i32 = (cold_read*100)/128;
                        temp_i32 = dpoint_cal(&iProfile.temperature.cold_calib[0],temp_i32);
                        iProfile.sys_status.BatTempetature = temp_i32/10;
                        tmp117_err_flag_inner = 0;
                        
//                        if(tmp117_err_flag_outer == 1)
//                        {
//                            cold_voltage = Thermocouple_Temperature2Voltage(temp_i32);      
//                            iProfile.temperature.ColdTemper = temp_i32/10;
//                        }                                            
                        
                    }
                    else
                    {                        
                        if(cold_read_error_inner++>5)
                        {
                            cold_read_error_inner = 0;                            
                            result = SEN_TMP117_write(0x90,0x01,0xC2);  //Reset chip
                            rt_thread_delay(50);
                            
                            if(tmp117_err_flag_inner == 0)
                            {
                                log_write(EFLOG_LVL_ERROR,LOG_TAG_HARD,LOG_HARD_INNER_TEMP_FAULT,__LINE__,0,0,0);
                            }
                            tmp117_err_flag_inner = 1;
                        }
                    }            
                }
                else
                {
                    cold_read_error_inner++;
                }
            }
            else
            {
                if(cold_read_error_inner++>5)
                {
                    cold_read_error_inner = 0;
                    result = SEN_TMP117_write(0x90,0x01,0xC2);  //Reset chip
                    rt_thread_delay(50);
                    
                    if(tmp117_err_flag_inner == 0)
                    {
                        log_write(EFLOG_LVL_ERROR,LOG_TAG_HARD,LOG_HARD_INNER_TEMP_FAULT,__LINE__,0,0,0);
                    }
                    tmp117_err_flag_inner = 1;
                }
            }  
            
        }
        
		ADS_GetThermocouple();

#if (USE_V_OXY_1 != 0)
        o2_sen_polling();
#endif
        
#if (USE_V_VACUUM_1 != 0)
        pressure_sen_polling();
#endif
		//rt_thread_delay(3);
#if (USE_VIBRATE != 0)
		vibration_polling();
#endif      
      
		test_meas_count++;
	}
}

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
const uint8_t tc_channel_map[TEMPERATURE_MAX_CHANNEL] = {0,5,8,9,2,3,6,10,1,4,7,11  ,12,13,14,15,16,17,18,19,20,21,22,23,24};
    
uint8_t ADS1220_Rdata(int32_t *pData)
{
	uint8_t OutData[4];
	int32_t Temp;
	uint8_t status;
   
    ADS_SendByte(0x12);
	ADS_SendByte(0x00);
	status = ADS_SendByte(0x00);
	
    OutData[0] = ADS_SendByte(0x00);
    OutData[1] = ADS_SendByte(0x00);
    OutData[2] = ADS_SendByte(0x00);
    
	Temp = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]); 
	
	if(Temp&0x800000)
	{
		Temp ^= 0xFFFFFF;
		Temp += 1;
        *pData = Temp *-1;
	}
	else
	{
		*pData = Temp;
	}
    return status;
}

void TC_ADS_WriteReg(uint8_t Addr,uint8_t Reg)
{
    ADS_SendByte(0x40 | Addr);
	ADS_SendByte(0x00);
    ADS_SendByte(Reg);
}

void TC_ADS_ReadReg(uint8_t Addr,uint8_t *Reg)
{
    ADS_SendByte(0x20 | Addr);
	ADS_SendByte(0x00);
    *Reg = ADS_SendByte(0xFF);
}


/*****************************************************************************************
* Function Name  : select_mux_channel
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void select_mux_channel(uint8_t ch)
{
    ADS_CS_1_L();
    ADS_CS_2_L();
    ADS_CS_3_L();
    ADS_CS_4_L();
    ADS_CS_5_L();
    ADS_CS_6_L();
    
	MEAS_uDelay(5);
		
	switch(ch)
	{
		case 0:ADS_WriteReg(0x00,0x5C);break;//2-3
		case 1:ADS_WriteReg(0x00,0x4C);break;//1-3
		case 2:ADS_WriteReg(0x00,0x2C);break;//0-3
		
		default:
		{
			ADS_WriteReg(0x00,0x2C);
		}break;
	}
    
    ADS_WriteReg(0x01,0xA4);    //330sps 
    
    MEAS_uDelay(5);
	ADS_CS_1_H();
    ADS_CS_2_H();
    ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
}
//

void select_adc_chip(uint8_t ch)
{
    ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
    ADS_nop();
    
    switch(ch)
    {
        case 0:ADS_CS_1_L();break;
        case 1:ADS_CS_2_L();break;
        case 2:ADS_CS_3_L();break;
        case 3:ADS_CS_4_L();break;
        case 4:ADS_CS_5_L();break;
        case 5:ADS_CS_6_L();break;
        case 6:ADS_CS_7_L();break;
        case 7:ADS_CS_8_L();break;
        
        default:
        {
            //ADS_CS_1_L();
        }break;
    }
    ADS_nop();
}

uint8_t test_buf[16];

void ADS_Reset(void)
{
	uint8_t i;
    uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t OutData[4];
    int32_t Temp_32;
    uint32_t offset_buf[32];
	
    ACC_CS_H();
    
	ADS_CS_1_L();
    ADS_CS_2_L();
    ADS_CS_3_L();
    ADS_CS_4_L();
    ADS_CS_5_L();
    ADS_CS_6_L();
    SEN_CS_L();

	MEAS_uDelay(10);
	ADS_SendByte(0x06);
	ADS_SendByte(0x00);
	MEAS_uDelay(10);
    
	ADS_CS_1_H();
    ADS_CS_2_H();
    ADS_CS_3_H();
    ADS_CS_4_H();
    ADS_CS_5_H();
    ADS_CS_6_H();
    SEN_CS_H();
    
	
	rt_thread_delay(20);
	
	ADS_CS_1_L();
    ADS_CS_2_L();
    ADS_CS_3_L();
    ADS_CS_4_L();
    ADS_CS_5_L();
    ADS_CS_6_L();
    
    SEN_CS_L();
    
	MEAS_uDelay(5);
	ADS_WriteReg(0x00,0xEC); //offset
	ADS_WriteReg(0x01,0xA4);//SPS = 600
	ADS_WriteReg(0x02,0x00);
	ADS_WriteReg(0x03,0x00);
	MEAS_uDelay(5);
    
	ADS_CS_1_H();
    ADS_CS_2_H();
    ADS_CS_3_H();
    ADS_CS_4_H();
    ADS_CS_5_H();
    ADS_CS_6_H();
    SEN_CS_H();
    
    
//    ADS_CS_1_L();
//	MEAS_uDelay(5);
//	ADS_ReadReg(0x00,&test_buf[0]);
//    ADS_ReadReg(0x01,&test_buf[1]);
//    ADS_ReadReg(0x02,&test_buf[2]);
//    ADS_ReadReg(0x03,&test_buf[3]);
//	MEAS_uDelay(5);
//	ADS_CS_1_H();
//    
//    MEAS_uDelay(10);

//    ADS_CS_2_L();
//	MEAS_uDelay(5);
//	ADS_ReadReg(0x00,&test_buf[4]);
//    ADS_ReadReg(0x01,&test_buf[5]);
//    ADS_ReadReg(0x02,&test_buf[6]);
//    ADS_ReadReg(0x03,&test_buf[7]);
//	MEAS_uDelay(5);
//	ADS_CS_2_H();
//    
//	MEAS_uDelay(10);
//	
//	ADS_CS_3_L();
//	MEAS_uDelay(5);
//	ADS_ReadReg(0x00,&test_buf[8]);
//    ADS_ReadReg(0x01,&test_buf[9]);
//    ADS_ReadReg(0x02,&test_buf[10]);
//    ADS_ReadReg(0x03,&test_buf[11]);
//	MEAS_uDelay(5);
//	ADS_CS_3_H();
//	
//	rt_thread_delay(20);
    
	ADS_CS_1_L();
    ADS_CS_2_L();
    ADS_CS_3_L();
    ADS_CS_4_L();
    ADS_CS_5_L();
    ADS_CS_6_L();
    SEN_CS_L();
    
    MEAS_uDelay(5);
	ADS_SendByte(0x08); //start
    MEAS_uDelay(5);
    
	ADS_CS_1_H();
    ADS_CS_2_H();
    ADS_CS_3_H();
    ADS_CS_4_H();
    ADS_CS_5_H();
    ADS_CS_6_H();
    SEN_CS_H();
    
    
	rt_thread_delay(20);
      
//    for(i=0;i<10;i++)
//    {       

//        ADS_CS_1_L();
//        
//        ADS_nop();		
//        
//        ADS_SendByte(0x10);	
//        OutData[0] = ADS_SendByte(0x00);
//        OutData[1] = ADS_SendByte(0x00);
//        OutData[2] = ADS_SendByte(0x00);
//        ADS_nop();	
//        
//        ADS_CS_1_H();
//    
//        Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
//    
//        if(Temp_32&0x800000)
//        {
//            Temp_32 |= 0xFF000000;
//        }
//        avg_buf[0][i] = Temp_32; 
//        
//        ADS_nop();
//        ADS_nop();
//        ADS_nop();
//        ADS_nop();
//        ADS_nop();
//        ADS_nop();
//        
//        SEN_CS_L();
//        
//        ADS_nop();		
//        
//        ADS_SendByte(0x10);	
//        OutData[0] = ADS_SendByte(0x00);
//        OutData[1] = ADS_SendByte(0x00);
//        OutData[2] = ADS_SendByte(0x00);
//        ADS_nop();	
//        
//        SEN_CS_H();
//    
//        Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
//    
//        if(Temp_32&0x800000)
//        {
//            Temp_32 |= 0xFF000000;
//        }
//        avg_buf[1][i] = Temp_32; 
//        
//        
//        rt_thread_delay(3);
//    }
//       

//    Temp_32 = 0;
//    for(i=0;i<10;i++)
//    {
//        Temp_32 += avg_buf[0][i];;
//    }
//    adc_offset_table[0] = Temp_32/10;
//    
//    if((adc_offset_table[0] > 3000)||(adc_offset_table[0] < -3000))
//    {
//        adc_offset_table[0] = 0;
//    }
//    
//    Temp_32 = 0;
//    for(i=0;i<10;i++)
//    {
//        Temp_32 += avg_buf[1][i];;
//    }
//    adc_offset_table[1] = Temp_32/10;
//    
//    if((adc_offset_table[1] > 3000)||(adc_offset_table[1] < -3000))
//    {
//       adc_offset_table[1] = 0;
//    }
    
    SEN_CS_L();
	ADS_nop();		
	ADS_WriteReg(0x00,0x70);  //AIN 2:3 /Gain=0;   
    ADS_WriteReg(0x01,0x24); 
    ADS_nop();	 
    SEN_CS_H();
    
    
    SEN_CS_L();
    
    MEAS_uDelay(5);
	ADS_SendByte(0x08); //start
    MEAS_uDelay(5);   
    SEN_CS_H();
    
    MEAS_uDelay(5);
    
    select_mux_channel(0);
    rt_thread_delay(20);   
   
}

uint32_t min_value;
uint32_t timeout_value;


void ADS_GetThermocouple(void)
{
    
	uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t avg_i,avg_j;
    int32_t Temp_32;
	int64_t Temp64;
	int16_t i,j;
    
	uint8_t ch_m;
	uint32_t Timeout;
	int32_t ADCData;
    uint8_t OutData[4];
	
	int16_t MinTemp;
	int16_t MaxTemp;
	int16_t MinChannel;
	int16_t MaxChannel;
	
	static int16_t MinJitterTimer;
	static int16_t MinJitterChannel;
	static int16_t MaxJitterTimer;
	static int16_t MaxJitterChannel;
    
    static int16_t insert_jitter_timer;
    static int16_t insert_jitter_channel;    
    
    static uint16_t cold_timer = 100;
    
    ACC_CS_H();
    SEN_CS_H();
    ADS_CS_1_H();	
    ADS_CS_2_H();	
    ADS_CS_3_H();	
    ADS_CS_4_H();	
    ADS_CS_5_H();	
    
    if(cold_timer++>20)
    {
        cold_timer = 0;
        
        ADS_CS_1_L();
        MEAS_uDelay(1);
        ADS_WriteReg(0x00,0x0C);
        ADS_WriteReg(0x01,0x86);
        MEAS_uDelay(1);
        ADS_CS_1_H();	
        
        ADS_CS_1_L();
        MEAS_uDelay(1);
        ADS_SendByte(0x08); //start
        MEAS_uDelay(1);
        ADS_CS_1_H();
        
        rt_thread_delay(20);
        
//        ADS_CS_3_L();
//        MEAS_uDelay(5);
//        ADS_ReadReg(0x00,&test_buf[0]);
//        ADS_ReadReg(0x01,&test_buf[1]);
//        ADS_ReadReg(0x02,&test_buf[2]);
//        MEAS_uDelay(5);
//        ADS_CS_3_H();
        
        
        for(average_i=0;average_i < AVG_TMP_LENGTH;average_i++)
        {
            ADS_CS_1_L();   
            
            ADS_nop();		
           
            ADS_SendByte(0x10);	
            OutData[0] = ADS_SendByte(0x00);
            OutData[1] = ADS_SendByte(0x00);
            OutData[2] = ADS_SendByte(0x00);
            ADS_nop();

            ADS_CS_1_H();            
            
            Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
        
            if(Temp_32 == 0xFFFFFF)
            {
                Temp_32 = 0x7FFFFF;
            }
            
            if(Temp_32&0x800000)
            {
                Temp_32 |= 0xFF000000;
            }
            avg_buf[0][average_i] = Temp_32;             

            rt_thread_delay(3);
        }  
        
        ADS_CS_1_L();
        MEAS_uDelay(1);
        ADS_WriteReg(0x00,0x0C);
        ADS_WriteReg(0x01,0xA4);
        MEAS_uDelay(1);
        ADS_CS_1_H();	
        
        MEAS_uDelay(1);
        
        ADS_CS_1_L();
        MEAS_uDelay(1);
        ADS_SendByte(0x08); //start
        MEAS_uDelay(1);
        ADS_CS_1_H();
                   
        for(avg_i=0;avg_i<AVG_TMP_LENGTH;avg_i++)
        {
            for(avg_j=avg_i+1;avg_j<AVG_TMP_LENGTH;avg_j++)
            {
                if(avg_buf[0][avg_i]>avg_buf[0][avg_j])
                {
                    Temp_32 = avg_buf[0][avg_i];
                    avg_buf[0][avg_i] = avg_buf[0][avg_j];
                    avg_buf[0][avg_j] = Temp_32;
                }
            }
        }
        
        Temp64 = 0;
        for(avg_i=AVG_TMP_LOSE;avg_i<(AVG_TMP_LENGTH-AVG_TMP_LOSE);avg_i++)
        {
            Temp64 += avg_buf[0][avg_i];
        }
        Temp_32 = Temp64/(AVG_TMP_LENGTH - AVG_TMP_LOSE - AVG_TMP_LOSE);
 
        Temp_32>>=10;
        if((Temp_32&(1<<13))!=0)
        {
            Temp_32|=0xFFFFC000;
        }
        
        Temp_32 = (Temp_32*3125)/1000;				
    
        if(Temp_32>=12800)
            Temp_32 = 12800;
        if(Temp_32<=0)
            Temp_32 = 0;
        
                    
        Temp_32 = dpoint_cal(&iProfile.temperature.cold_calib[0],Temp_32);
        cold_voltage = Thermocouple_Temperature2Voltage(Temp_32);      
        iProfile.temperature.ColdTemper = Temp_32/10;
        iProfile.sys_status.BatTempetature = iProfile.temperature.ColdTemper;                              
    }
            
    for(chip_ch=0;chip_ch<3;chip_ch++)
    {
        select_mux_channel(chip_ch);
        rt_thread_delay(4);
        
        for(average_i=0;average_i < AVG_TMP_LENGTH;average_i++)
        {
            for(InCh = 0;InCh<4;InCh++)
            {
                ADS_nop();                
                select_adc_chip(InCh);               
                ADS_nop();		
                
                ADS_SendByte(0x10);	
                OutData[0] = ADS_SendByte(0x00);
                OutData[1] = ADS_SendByte(0x00);
                OutData[2] = ADS_SendByte(0x00);
                ADS_nop();	
                
                Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
                
                ADS_nop();
                
                ADS_CS_1_H();
                ADS_CS_2_H();
                ADS_CS_3_H();
                ADS_CS_4_H();
                ADS_CS_5_H();
                ADS_CS_6_H();
                ADS_CS_7_H();
                ADS_CS_8_H();
            
                if(Temp_32 == 0xFFFFFF)
                {
                    Temp_32 = 0x7FFFFF;
                }
                
                if(Temp_32&0x800000)
                {
                    Temp_32 |= 0xFF000000;
                }
                avg_buf[InCh][average_i] = Temp_32; 	                
            }
            rt_thread_delay(2);
        }
        
        for(i=0;i<4;i++)
        {
            for(avg_i=0;avg_i<AVG_TMP_LENGTH;avg_i++)
            {
                for(avg_j=avg_i+1;avg_j<AVG_TMP_LENGTH;avg_j++)
                {
                    if(avg_buf[i][avg_i]>avg_buf[i][avg_j])
                    {
                        Temp_32 = avg_buf[i][avg_i];
                        avg_buf[i][avg_i] = avg_buf[i][avg_j];
                        avg_buf[i][avg_j] = Temp_32;
                    }
                }
            }
            Temp64 = 0;
            for(avg_i=AVG_TMP_LOSE;avg_i<(AVG_TMP_LENGTH-AVG_TMP_LOSE);avg_i++)
            {
                Temp64 += avg_buf[i][avg_i];
            }
            Temp64 = Temp64/(AVG_TMP_LENGTH - AVG_TMP_LOSE - AVG_TMP_LOSE);
     
            ch_m = tc_channel_map[chip_ch*4 + i];
            
            if(Temp64 < 8323072)
            {
                //Temp64 = Temp64 - adc_offset_table[i];
                ADCData = (Temp64*3125)>>13;
//                if(ch_m == 19)
//                {
//                    ch_m++;
//                }
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 1;
                ADCData = ADCData + cold_voltage;
                //ADCData = kalmanFilter_calc(&meas_kalman[ch_m],ADCData);
                iProfile.temperature.temperature[ch_m] = dpoint_cal(&iProfile.temperature.calib[ch_m],Thermocouple_Voltage2Temperature(ADCData));
            }
            else
            {
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 0;
                iProfile.temperature.temperature[ch_m]  = -999;
                kalmanFilter_init(&meas_kalman[ch_m],0,0.15,0.4);
            }        
        }	     
    }       
	
	MaxTemp = -999;
	MinTemp = 9999;
	
	for(InCh = 0;InCh<TEMPERATURE_MAX_CHANNEL-2  ;InCh++)
	{	
		if(InCh >= iProfile.conf.Total_channel)
		{
			break;
		}
		if(iProfile.temperature.ThermcoupleConnectStatus[InCh]!=0)
		{
			if(iProfile.temperature.temperature[InCh]>MaxTemp)
			{
				MaxChannel = InCh + 1;
				MaxTemp = iProfile.temperature.temperature[InCh];
			}
			if(iProfile.temperature.temperature[InCh]<MinTemp)
			{
				MinChannel = InCh + 1;
				MinTemp = iProfile.temperature.temperature[InCh];
			}
		}		
	}
	
	InCh=0;
	j=0;
	for(i=0;i<TEMPERATURE_MAX_CHANNEL-2;i++)
	{
		if(i >= iProfile.conf.Total_channel)
		{
			iProfile.temperature.ThermcoupleConnectStatus[i] = 0;
		}
        else
        {
            if(ThermcoupleConnectStatus_Last[i] != iProfile.temperature.ThermcoupleConnectStatus[i])
            {
                ThermcoupleConnectStatus_Last[i] = iProfile.temperature.ThermcoupleConnectStatus[i];
                if(ThermcoupleConnectStatus_Last[i] != 0)
                {
                    j = i + 1;
                }
            }
            
            if(iProfile.temperature.ThermcoupleConnectStatus[i]!=0)
            {
                InCh++;
            }	
        }
	}
	iProfile.temperature.total_connect  = InCh;
    
    if(j!=0)
    {
        insert_jitter_timer = 0;
        insert_jitter_channel = j;
    }
    
    if(insert_jitter_channel != 0)
    {
        if(iProfile.temperature.ThermcoupleConnectStatus[insert_jitter_channel - 1] !=0)
        {
            if(insert_jitter_timer++>3)
            {                
                insert_jitter_timer = 0;
                iProfile.temperature.new_inster_flag = 1;
                iProfile.temperature.last_inster = insert_jitter_channel-1;
                insert_jitter_channel = 0;
            }
        }
        else
        {
            insert_jitter_channel = 0;
        }
    }
    
	
	if(MinJitterChannel!=MinChannel)
	{
		MinJitterTimer = 0;
		MinJitterChannel = MinChannel;
		iProfile.temperature.MinTempChannel = 0;
	}
	else if(MinJitterTimer++>5)	//2s
	{
		MinJitterTimer = 30;
		iProfile.temperature.MinTempChannel = MinChannel;
		iProfile.temperature.MinTemperature = MinTemp;
	}
	
	if(MaxJitterChannel!=MaxChannel)
	{
		MaxJitterTimer = 0;
		MaxJitterChannel = MaxChannel;
		iProfile.temperature.MaxTempChannel = 0;
	}
	else if(MaxJitterTimer++>5)	//2s
	{
		MaxJitterTimer = 30;
		iProfile.temperature.MaxTempChannel = MaxChannel;
		iProfile.temperature.MaxTemperature = MaxTemp;
	}
}

int test_meas_count;

static void Task_MEAS_Polling(void *parameter)
{  
	
    int16_t cold_read;
    uint8_t result;     
    
    
	while(1)
	{                      
        ADS_GetThermocouple();

#if (USE_V_OXY_1 != 0)
        o2_sen_polling();
#endif
        
#if (USE_V_VACUUM_1 != 0)
        pressure_sen_polling();
#endif
		//rt_thread_delay(3);
#if (USE_VIBRATE != 0)
		vibration_polling();
#endif      
      
		test_meas_count++;
	}
} 

#elif DEV_TYPE == DEV_TYPE_RPT_STD

const uint8_t tc_channel_map[TEMPERATURE_MAX_CHANNEL] = {1,0,2,3,4,5,6,3,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
    
uint8_t ADS1220_Rdata(int32_t *pData)
{
	uint8_t OutData[4];
	int32_t Temp;
	uint8_t status;
   
    ADS_SendByte(0x12);
	ADS_SendByte(0x00);
	status = ADS_SendByte(0x00);
	
    OutData[0] = ADS_SendByte(0x00);
    OutData[1] = ADS_SendByte(0x00);
    OutData[2] = ADS_SendByte(0x00);
    
	Temp = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]); 
	
	if(Temp&0x800000)
	{
		Temp ^= 0xFFFFFF;
		Temp += 1;
        *pData = Temp *-1;
	}
	else
	{
		*pData = Temp;
	}
    return status;
}

void TC_ADS_WriteReg(uint8_t Addr,uint8_t Reg)
{
    ADS_SendByte(0x40 | Addr);
	ADS_SendByte(0x00);
    ADS_SendByte(Reg);
}

void TC_ADS_ReadReg(uint8_t Addr,uint8_t *Reg)
{
    ADS_SendByte(0x20 | Addr);
	ADS_SendByte(0x00);
    *Reg = ADS_SendByte(0xFF);
}


/*****************************************************************************************
* Function Name  : select_mux_channel
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void select_mux_channel(uint8_t ch)
{
    ADS_CS_1_L();
    ADS_CS_2_L();
	MEAS_uDelay(5);
		
	switch(ch)
	{
		case 0:ADS_WriteReg(0x00,0x5C);break;//2-3
		case 1:ADS_WriteReg(0x00,0x4C);break;//1-3
		case 2:ADS_WriteReg(0x00,0x2C);break;//0-3
		
		default:
		{
			ADS_WriteReg(0x00,0x2C);
		}break;
	}
    
    ADS_WriteReg(0x01,0xA4);    //330sps 
    MEAS_uDelay(5);
	ADS_CS_1_H();
    ADS_CS_2_H();
}

void select_adc_chip(uint8_t ch)
{
    ADS_CS_1_H();
	ADS_CS_2_H();
	ADS_CS_3_H();
	ADS_CS_4_H();
    ADS_CS_5_H();
	ADS_CS_6_H();
    ADS_CS_7_H();
	ADS_CS_8_H();
    ADS_nop();
    
    switch(ch)
    {
        case 0:ADS_CS_1_L();break;
        case 1:ADS_CS_2_L();break;
        case 2:ADS_CS_3_L();break;
        case 3:ADS_CS_4_L();break;
        case 4:ADS_CS_5_L();break;
        case 5:ADS_CS_6_L();break;
        case 6:ADS_CS_7_L();break;
        case 7:ADS_CS_8_L();break;
        
        default:
        {
            ADS_CS_1_L();
        }break;
    }
    ADS_nop();
}


uint8_t test_buf[32];
void ADS_Reset(void)
{
	uint8_t i;
    uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t OutData[4];
    int32_t Temp_32;
    uint32_t offset_buf[32];
	
	ADS_CS_1_L();
    ADS_CS_2_L();

	MEAS_uDelay(10);
	ADS_SendByte(0x06);
	ADS_SendByte(0x00);
	MEAS_uDelay(10);
    
	ADS_CS_1_H();
    ADS_CS_2_H();
	
	rt_thread_delay(20);
	
	ADS_CS_1_L();
    ADS_CS_2_L();
    
	MEAS_uDelay(5);
	ADS_WriteReg(0x00,0xEC); //offset
	ADS_WriteReg(0x01,0xA4);//SPS = 600
	ADS_WriteReg(0x02,0x00);
	ADS_WriteReg(0x03,0x00);
	MEAS_uDelay(5);
    
	ADS_CS_1_H();
    ADS_CS_2_H();


//	MEAS_uDelay(10000);
	
	ADS_CS_1_L();
	MEAS_uDelay(5);
	ADS_ReadReg(0x00,&test_buf[0]);
	MEAS_uDelay(5);
	ADS_CS_1_H();
	
	rt_thread_delay(20);
    
	ADS_CS_1_L();
    ADS_CS_2_L();
    
    MEAS_uDelay(5);
	ADS_SendByte(0x08); //start
    MEAS_uDelay(5);
    
	ADS_CS_1_H();
    ADS_CS_2_H();

    
	rt_thread_delay(20);
      
    for(i=0;i<10;i++)
    {       

        ADS_CS_1_L();
        
        ADS_nop();		
        
        ADS_SendByte(0x10);	
        OutData[0] = ADS_SendByte(0x00);
        OutData[1] = ADS_SendByte(0x00);
        OutData[2] = ADS_SendByte(0x00);
        ADS_nop();	
        
        ADS_CS_1_H();
    
        Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
    
        if(Temp_32&0x800000)
        {
            Temp_32 |= 0xFF000000;
        }
        avg_buf[0][i] = Temp_32; 

        ADS_CS_2_L();
        
        ADS_nop();		
        
        ADS_SendByte(0x10);	
        OutData[0] = ADS_SendByte(0x00);
        OutData[1] = ADS_SendByte(0x00);
        OutData[2] = ADS_SendByte(0x00);
        ADS_nop();	
        
        ADS_CS_2_H();
    
        Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
    
        if(Temp_32&0x800000)
        {
            Temp_32 |= 0xFF000000;
        }
        avg_buf[1][i] = Temp_32; 
        
        rt_thread_delay(3);
    }
       

    Temp_32 = 0;
    for(i=0;i<10;i++)
    {
        Temp_32 += avg_buf[0][i];;
    }
    adc_offset_table[0] = Temp_32/10;
    
    if((adc_offset_table[0] > 3000)||(adc_offset_table[0] < -3000))
    {
        adc_offset_table[0] = 0;
    }
    
    Temp_32 = 0;
    for(i=0;i<10;i++)
    {
        Temp_32 += avg_buf[1][i];;
    }
    adc_offset_table[1] = Temp_32/10;
    
    if((adc_offset_table[1] > 3000)||(adc_offset_table[1] < -3000))
    {
        adc_offset_table[1] = 0;
    }
    
    select_mux_channel(0);
    rt_thread_delay(20);   
   
}

uint32_t min_value;
uint32_t timeout_value;

void ADS_GetThermocouple(void)
{
    
	uint8_t InCh;
    uint8_t chip_ch;
    uint8_t average_i;
    uint8_t avg_i,avg_j;
    int32_t Temp_32;
	int64_t Temp64;
	int16_t i,j;
    
	uint8_t ch_m;
	uint32_t Timeout;
	int32_t ADCData;
    uint8_t OutData[4];
	
	int16_t MinTemp;
	int16_t MaxTemp;
	int16_t MinChannel;
	int16_t MaxChannel;
	
	static int16_t MinJitterTimer;
	static int16_t MinJitterChannel;
	static int16_t MaxJitterTimer;
	static int16_t MaxJitterChannel;
    
    static int16_t insert_jitter_timer;
    static int16_t insert_jitter_channel;    
    
    static uint16_t cold_timer = 100;
    
    if(cold_timer++>20)
    {
        cold_timer = 0;
        
        ADS_CS_1_L();
        MEAS_uDelay(1);
        ADS_WriteReg(0x00,0x0C);
        ADS_WriteReg(0x01,0x86);
        MEAS_uDelay(1);
        ADS_CS_1_H();	
        
        ADS_CS_1_L();
        MEAS_uDelay(1);
        ADS_SendByte(0x08); //start
        MEAS_uDelay(1);
        ADS_CS_1_H();
        
        rt_thread_delay(20);
        
        ADS_CS_1_L();
        MEAS_uDelay(5);
        ADS_ReadReg(0x00,&test_buf[0]);
        ADS_ReadReg(0x01,&test_buf[1]);
        ADS_ReadReg(0x02,&test_buf[2]);
        MEAS_uDelay(5);
        ADS_CS_1_H();
        
        
        for(average_i=0;average_i < AVG_TMP_LENGTH;average_i++)
        {
            ADS_CS_1_L();   
            
            ADS_nop();		
           
            ADS_SendByte(0x10);	
            OutData[0] = ADS_SendByte(0x00);
            OutData[1] = ADS_SendByte(0x00);
            OutData[2] = ADS_SendByte(0x00);
            ADS_nop();

            ADS_CS_1_H();            
            
            Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
        
            if(Temp_32 == 0xFFFFFF)
            {
                Temp_32 = 0x7FFFFF;
            }
            
            if(Temp_32&0x800000)
            {
                Temp_32 |= 0xFF000000;
            }
            avg_buf[0][average_i] = Temp_32;             

            rt_thread_delay(2);
        }  

        
            
        for(avg_i=0;avg_i<AVG_TMP_LENGTH;avg_i++)
        {
            for(avg_j=avg_i+1;avg_j<AVG_TMP_LENGTH;avg_j++)
            {
                if(avg_buf[0][avg_i]>avg_buf[0][avg_j])
                {
                    Temp_32 = avg_buf[0][avg_i];
                    avg_buf[0][avg_i] = avg_buf[0][avg_j];
                    avg_buf[0][avg_j] = Temp_32;
                }
            }
        }
        
        Temp64 = 0;
        for(avg_i=AVG_TMP_LOSE;avg_i<(AVG_TMP_LENGTH-AVG_TMP_LOSE);avg_i++)
        {
            Temp64 += avg_buf[0][avg_i];
        }
        Temp_32 = Temp64/(AVG_TMP_LENGTH - AVG_TMP_LOSE - AVG_TMP_LOSE);
 
        Temp_32>>=10;
        if((Temp_32&(1<<13))!=0)
        {
            Temp_32|=0xFFFFC000;
        }
        
        Temp_32 = (Temp_32*3125)/1000;				
    
        if(Temp_32>=12800)
            Temp_32 = 12800;
        if(Temp_32<=0)
            Temp_32 = 0;
        
                    
        Temp_32 = dpoint_cal(&iProfile.temperature.cold_calib[0],Temp_32);
        cold_voltage = Thermocouple_Temperature2Voltage(Temp_32);      
        iProfile.temperature.ColdTemper = Temp_32/10;
        iProfile.sys_status.BatTempetature = iProfile.temperature.ColdTemper;                              
    }
         
	ACC_CS_H();
    
    
    for(chip_ch=0;chip_ch<3;chip_ch++)
    {
        select_mux_channel(chip_ch);
        rt_thread_delay(4);
        
        for(average_i=0;average_i < AVG_TMP_LENGTH;average_i++)
        {
            for(InCh = 0;InCh<2;InCh++)
            {
                select_adc_chip(InCh);
                
                ADS_nop();		
                
                ADS_SendByte(0x10);	
                OutData[0] = ADS_SendByte(0x00);
                OutData[1] = ADS_SendByte(0x00);
                OutData[2] = ADS_SendByte(0x00);
                ADS_nop();	
                
                select_adc_chip(0xFF);//None
                
                Temp_32 = (((uint32_t)OutData[0] << 16) + ((uint32_t)OutData[1] << 8) + (uint32_t)OutData[2]);
            
                if(Temp_32 == 0xFFFFFF)
                {
                    Temp_32 = 0x7FFFFF;
                }
                
                if(Temp_32&0x800000)
                {
                    Temp_32 |= 0xFF000000;
                }
                avg_buf[InCh][average_i] = Temp_32;             
            }
            rt_thread_delay(1);
        }
        
        
        
        for(i=0;i<2;i++)
        {
            for(avg_i=0;avg_i<AVG_TMP_LENGTH;avg_i++)
            {
                for(avg_j=avg_i+1;avg_j<AVG_TMP_LENGTH;avg_j++)
                {
                    if(avg_buf[i][avg_i]>avg_buf[i][avg_j])
                    {
                        Temp_32 = avg_buf[i][avg_i];
                        avg_buf[i][avg_i] = avg_buf[i][avg_j];
                        avg_buf[i][avg_j] = Temp_32;
                    }
                }
            }
            Temp64 = 0;
            for(avg_i=AVG_TMP_LOSE;avg_i<(AVG_TMP_LENGTH-AVG_TMP_LOSE);avg_i++)
            {
                Temp64 += avg_buf[i][avg_i];
            }
            Temp64 = Temp64/(AVG_TMP_LENGTH - AVG_TMP_LOSE - AVG_TMP_LOSE);
     
            ch_m = tc_channel_map[chip_ch*2 + i];
            
            if(Temp64 < 8323072)
            {
                Temp64 = Temp64 - adc_offset_table[i];
                ADCData = (Temp64*3125)>>13;
//                if(ch_m == 19)
//                {
//                    ch_m++;
//                }
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 1;
                ADCData = ADCData + cold_voltage;
                //ADCData = kalmanFilter_calc(&meas_kalman[ch_m],ADCData);
                iProfile.temperature.temperature[ch_m] = dpoint_cal(&iProfile.temperature.calib[ch_m],Thermocouple_Voltage2Temperature(ADCData));
            }
            else
            {
                iProfile.temperature.ThermcoupleConnectStatus[ch_m] = 0;
                iProfile.temperature.temperature[ch_m]  = -999;
                kalmanFilter_init(&meas_kalman[ch_m],0,0.15,0.4);
            }        
        }	     
    }     
	
	MaxTemp = -999;
	MinTemp = 9999;
	
	for(InCh = 0;InCh<TEMPERATURE_MAX_CHANNEL-2  ;InCh++)
	{	
		if(InCh >= iProfile.conf.Total_channel)
		{
			break;
		}
		if(iProfile.temperature.ThermcoupleConnectStatus[InCh]!=0)
		{
			if(iProfile.temperature.temperature[InCh]>MaxTemp)
			{
				MaxChannel = InCh + 1;
				MaxTemp = iProfile.temperature.temperature[InCh];
			}
			if(iProfile.temperature.temperature[InCh]<MinTemp)
			{
				MinChannel = InCh + 1;
				MinTemp = iProfile.temperature.temperature[InCh];
			}
		}		
	}
	
	InCh=0;
	j=0;
	for(i=0;i<TEMPERATURE_MAX_CHANNEL-2;i++)
	{
		if(i >= iProfile.conf.Total_channel)
		{
			iProfile.temperature.ThermcoupleConnectStatus[i] = 0;
		}
        else
        {
            if(ThermcoupleConnectStatus_Last[i] != iProfile.temperature.ThermcoupleConnectStatus[i])
            {
                ThermcoupleConnectStatus_Last[i] = iProfile.temperature.ThermcoupleConnectStatus[i];
                if(ThermcoupleConnectStatus_Last[i] != 0)
                {
                    j = i + 1;
                }
            }
            
            if(iProfile.temperature.ThermcoupleConnectStatus[i]!=0)
            {
                InCh++;
            }	
        }
	}
	iProfile.temperature.total_connect  = InCh;
    
    if(j!=0)
    {
        insert_jitter_timer = 0;
        insert_jitter_channel = j;
    }
    
    if(insert_jitter_channel != 0)
    {
        if(iProfile.temperature.ThermcoupleConnectStatus[insert_jitter_channel - 1] !=0)
        {
            if(insert_jitter_timer++>3)
            {                
                insert_jitter_timer = 0;
                iProfile.temperature.new_inster_flag = 1;
                iProfile.temperature.last_inster = insert_jitter_channel-1;
                insert_jitter_channel = 0;
            }
        }
        else
        {
            insert_jitter_channel = 0;
        }
    }
    
	
	if(MinJitterChannel!=MinChannel)
	{
		MinJitterTimer = 0;
		MinJitterChannel = MinChannel;
		iProfile.temperature.MinTempChannel = 0;
	}
	else if(MinJitterTimer++>5)	//2s
	{
		MinJitterTimer = 30;
		iProfile.temperature.MinTempChannel = MinChannel;
		iProfile.temperature.MinTemperature = MinTemp;
	}
	
	if(MaxJitterChannel!=MaxChannel)
	{
		MaxJitterTimer = 0;
		MaxJitterChannel = MaxChannel;
		iProfile.temperature.MaxTempChannel = 0;
	}
	else if(MaxJitterTimer++>5)	//2s
	{
		MaxJitterTimer = 30;
		iProfile.temperature.MaxTempChannel = MaxChannel;
		iProfile.temperature.MaxTemperature = MaxTemp;
	}
}

int test_meas_count;

static void Task_MEAS_Polling(void *parameter)
{  
	
    int16_t cold_read;
    uint8_t result;     
    	
    
	while(1)
	{                      
        ADS_GetThermocouple();

		//rt_thread_delay(3);
#if (USE_VIBRATE != 0)
		vibration_polling();
#endif      
      
		test_meas_count++;
	}
} 
#else 
#error
#endif






/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int MEAS_Start(void)
{   
	uint16_t i;

	ADS_Reset();
	
	for(i=0;i<TEMPERATURE_MAX_CHANNEL;i++)
    {
        iProfile.temperature.temperature[i] = 0xFFFF;
    }
	
    kalmanFilter_init(&meas_kalman_cold,0,0.2,0.4);
    
    
	for(i=0;i<(TEMPERATURE_MAX_CHANNEL+1);i++)
	{
		kalmanFilter_init(&meas_kalman[i],0,0.2,0.4);
	}
    
#if (USE_V_VACUUM_1 != 0)
    xgzp6828d_init();
#endif	

#if (USE_VIBRATE != 0)
	vibration_init();
#endif
	
    // 创建静态任务
    meas_thread = xTaskCreateStatic(
        Task_MEAS_Polling,       // 任务函数
        "MEAS_Polling",             // 任务名称 (用于调试)
        sizeof(meas_stack) / sizeof(StackType_t), // 栈大小 (以StackType_t为单位)
        NULL,                       // 传递给任务的参数
        TD_PRI_LOW,                          // 任务优先级
        meas_stack,          // 静态栈缓冲区
        &meas_thread_tcb            // 静态任务控制块
    );
    if(meas_thread == NULL)
	{
		//Error_Handler();
        while(1);
	}
	
	test_sys_buf[7] = meas_thread;
		
	return (meas_thread != NULL) ? pdPASS : pdFAIL;
}

/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/
